:root {
  --bg: #ffffff;
  --fg: #222222;
  --muted: #666666;
  --border: #e6e6e6;
  --primary: #0b5fff;
  --danger: #c62828;
  --panel: #fafafa;
  --shadow: 0 10px 30px rgba(0,0,0,0.08);
  --topbar-height: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --fg: #e6e6e6;
    --muted: #9aa0a6;
    --border: #2a2f3a;
    --primary: #5b8cff;
    --danger: #ff6b6b;
    --panel: #151922;
    --shadow: 0 10px 30px rgba(0,0,0,0.35);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); font-family: -apple-system, BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","PingFang SC","Microsoft YaHei UI","Hiragino Sans GB","WenQuanYi Micro Hei",sans-serif; }
a { color: var(--primary); text-decoration: none; }
.container { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* Topbar: 强制单行与横向滚动 */
.topbar {
  display: flex !important;
  align-items: center !important;
  gap: 10px !important;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  position: sticky; top: 0; z-index: 100;
  flex-wrap: nowrap !important;
  white-space: nowrap !important;
  height: var(--topbar-height);
  box-sizing: border-box;
}
.topbar h1 {
  margin: 0; font-size: 18px;
  flex: 1 1 auto; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.actions {
  display: flex !important;
  gap: 10px;
  align-items: center;
  flex-wrap: nowrap !important;
  white-space: nowrap !important;
  overflow-x: auto; overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  flex: 0 1 auto;
  max-width: 70vw;
}
.actions > * { flex: 0 0 auto !important; }
.actions form { display: inline-flex; margin: 0; }
.actions form .btn { 
  background: transparent !important; 
  color: var(--fg) !important; 
  border: 1px solid var(--border) !important;
  font-weight: normal !important;
}
.actions form .btn:hover { 
  border-color: var(--muted) !important; 
  background: color-mix(in srgb, var(--panel) 70%, transparent) !important; 
}

/* 统一按钮尺寸 */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; font-size: 14px; white-space: nowrap;
  border: 1px solid var(--border); border-radius: 10px; background: transparent; color: var(--fg);
  cursor: pointer; transition: .15s border-color,.15s background-color;
  flex: 0 0 auto !important;
  text-decoration: none;
}
.btn:hover { border-color: var(--muted); background: color-mix(in srgb, var(--panel) 70%, transparent); }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 16px; margin: 16px 0; box-shadow: var(--shadow); }
.panel h2 { margin: 0 0 12px; font-size: 16px; }

.alert { padding: 10px 12px; border-radius: 10px; margin: 10px 0; border: 1px solid var(--border); }
.alert.success { background: rgba(16,185,129,0.10); }
.alert.error { background: rgba(244,67,54,0.10); }

.list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
.item { border: 1px solid var(--border); border-radius: 12px; padding: 12px; background: var(--bg); display: flex; flex-direction: column; gap: 8px; }
.meta .title { font-weight: 600; }
.meta .label { color: var(--muted); }
.meta .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }

.codebox { display: flex; align-items: baseline; gap: 10px; }
.code { font-size: 28px; font-weight: 700; letter-spacing: 2px; font-variant-numeric: tabular-nums; }
.remain { color: var(--muted); }

.ops { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
details { padding: 6px 0; }
details .qr { display: block; width: 160px; height: 160px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
details .uri { font-family: ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace; font-size: 12px; word-break: break-all; color: var(--muted); margin-top: 6px; }

.field { display: flex; flex-direction: column; gap: 3px; margin-bottom: 10px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 500; }
/* 紧凑布局的行内字段 */
.field.compact { margin-bottom: 6px; }
.field.inline { flex-direction: row; align-items: center; gap: 10px; }
.field.inline label { min-width: 75px; margin-bottom: 0; }
input, select, textarea { 
  background: var(--bg); 
  color: var(--fg); 
  border: 1px solid var(--border); 
  border-radius: 6px; 
  padding: 8px 12px; 
  font-size: 14px; /* iOS 防止缩放 */
  line-height: 1.4;
  width: 100%;
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;
}
/* 弹窗内的文本域优化尺寸 */
.modal textarea { 
  min-height: 120px; 
  max-height: 240px; 
  resize: vertical;
  word-wrap: break-word;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  line-height: 1.4;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(66, 133, 244, 0.1);
}
/* iOS 密码输入优化 - 使用文本输入框但显示为密码 */
input[style*="text-security"] {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  letter-spacing: normal;
  -webkit-text-security: disc !important;
  text-security: disc !important;
}
textarea { resize: vertical; }
/* 限制弹窗内文本域高度，避免撑出屏幕 */
.modal textarea { max-height: 40vh; }
@supports (height: 1dvh) { .modal textarea { max-height: 40dvh; } }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.footer { text-align: center; color: var(--muted); padding: 20px 0 40px; }

/* Scan panel */
.scan video { width: 100%; max-height: 260px; background: #000; border: 1px solid var(--border); border-radius: 10px; margin-top: 8px; }
.scan .controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Modal */
.modal { position: fixed; inset: 0; display: none; }
.modal[aria-hidden="false"] { display: block; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.35); }
/* 让弹窗显示在导航栏下方 */
.modal { top: var(--topbar-height, 60px); }
.modal__backdrop { top: var(--topbar-height, 60px); }
.modal__dialog {
  position: relative;
  z-index: 1;
  width: min(640px, 95vw);
  margin: 1.5vh auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px;
  /* 更紧凑的高度规格 */
  min-height: 480px;
  max-height: calc(100vh - var(--topbar-height) - 3vh - env(safe-area-inset-bottom));
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
/* 所有弹窗现在使用完全一致的样式，无需额外覆盖 */
/* 支持 dvh 的浏览器使用动态视口高度 */
@supports (height: 1dvh) {
  .modal__dialog {
    max-height: calc(100dvh - var(--topbar-height) - 4dvh - env(safe-area-inset-bottom));
    margin: 2dvh auto;
  }
}
/* 弹窗内容区域 */
.modal__content {
  flex: 1;
  overflow: visible;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 12px;
}
.modal__close { position: absolute; top: 16px; right: 16px; border: 1px solid var(--border); border-radius: 999px; width: 32px; height: 32px; background: transparent; color: var(--fg); cursor: pointer; transition: all 0.2s ease; }
.modal__close:hover {
  background: var(--border);
  border-color: var(--muted);
}

.modal h3 { 
  margin: 0 0 18px 0; 
  font-size: 18px; 
  font-weight: 600; 
  color: var(--fg);
  flex-shrink: 0;
  line-height: 1.3;
}

/* 导出弹窗使用统一的基础样式 */

/* 提示信息卡片式设计 */
.modal .hint { 
  background: var(--bg); 
  border: 1px solid var(--border); 
  border-radius: 8px; 
  padding: 12px 16px; 
  margin-bottom: 0;
  line-height: 1.5;
  font-size: 13px;
  color: var(--muted);
  flex-shrink: 0;
}

#exportModal .hint { margin-bottom: 0 !important; }
#exportModal textarea#exportData { height: clamp(160px, 28vh, 200px); max-height: clamp(160px, 28vh, 220px); }
.modal__actions { 
  display: flex; 
  justify-content: flex-end; 
  gap: 10px; 
  margin-top: auto;
  padding-top: 16px; 
  border-top: 1px solid var(--border); 
  flex-shrink: 0;
  background: var(--panel);
  position: sticky;
  bottom: 0;
}

/* Tabs */
.tabs { 
  display: flex; 
  gap: 6px; 
  margin: 0 0 16px 0; 
  flex-shrink: 0;
}
.tab { 
  border: 1px solid var(--border); 
  background: transparent; 
  color: var(--fg); 
  border-radius: 16px; 
  padding: 8px 16px; 
  cursor: pointer; 
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s ease;
}
.tab.active { 
  background: var(--primary); 
  color: #fff; 
  border-color: var(--primary); 
}
.tab:hover:not(.active) {
  border-color: var(--primary);
  background: rgba(11, 95, 255, 0.05);
}
.tabpanes { 
  margin-top: 8px; 
  flex: 1;
  min-height: 0;
  overflow: visible;
}
.tabpane { 
  display: none; 
  height: 100%;
}
.tabpane.active { 
  display: block; 
}

/* 小屏优化 */
@media (max-width: 420px) {
  .topbar h1 { font-size: 16px; }
  .btn { height: 32px; padding: 0 10px; font-size: 13px; }
  .actions { gap: 8px; max-width: 64vw; }
  
  /* 移动端弹窗优化 */
  .modal__dialog {
    margin: 0.5vh auto;
    /* 保持与桌面端一致的宽度 */
    width: min(640px, 97vw);
    /* 小屏优先使用 vh 作为回退，减去导航栏高度 */
    max-height: calc(100vh - var(--topbar-height) - 1vh - env(safe-area-inset-bottom));
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    /* 确保内容区域有足够滚动空间 */
    display: flex;
    flex-direction: column;
  }
  /* 只有密码弹窗使用较小尺寸 */
  #unlockModal .modal__dialog,
  #setupMasterModal .modal__dialog {
    width: min(480px, 96vw) !important;
  }
  .modal__dialog > * {
    flex-shrink: 0;
  }
  .modal__dialog .tabpanes {
    flex: 1;
    min-height: 0;
    overflow: auto;
  }
  @supports (height: 1dvh) {
    .modal__dialog {
      margin: 1dvh auto;
      max-height: calc(100dvh - var(--topbar-height) - 2dvh - env(safe-area-inset-bottom));
    }
    /* 确保所有主要弹窗在dvh支持下使用统一尺寸 */
    #addModal .modal__dialog,
    #importModal .modal__dialog,
    #exportModal .modal__dialog {
      margin: 2dvh auto !important;
      max-height: calc(100dvh - var(--topbar-height) - 4dvh - env(safe-area-inset-bottom)) !important;
      min-height: 480px !important;
    }
  }
  /* 移动端弹窗统一优化 */
  .modal__dialog { 
    overflow: auto;
    padding: 16px;
    min-height: 480px;
  }
  
  .modal .hint { 
    margin-bottom: 0 !important; 
  }
  
  .modal textarea {
    height: clamp(100px, 25vh, 160px);
    max-height: clamp(100px, 25vh, 180px);
  }

  /* 密码弹窗特别优化 */
  #unlockModal .modal__dialog,
  #setupMasterModal .modal__dialog {
    margin: 8vh auto;
    max-height: 80vh;
    width: min(480px, 95vw);
  }
  /* 移动端视口适配 */
  @media (max-height: 600px) {
    .modal__dialog {
      margin: 0.5vh auto;
      max-height: 98vh;
    }
    #unlockModal .modal__dialog,
    #setupMasterModal .modal__dialog {
      margin: 2vh auto;
      max-height: 95vh;
    }
  }
  .field { margin-bottom: 12px; }
  .field label { 
    font-size: 13px; 
    font-weight: 500;
    margin-bottom: 6px;
    display: block;
  }
  input, select, textarea {
    padding: 12px 14px;
    font-size: 16px;
    border-radius: 10px;
    min-height: 42px; /* iOS 触摸目标 */
  }
  .modal__actions {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid var(--border);
  }
  .modal__actions .btn {
    height: 42px;
    font-size: 15px;
    font-weight: 500;
    border-radius: 10px;
    min-width: 100px;
  }
}

