/* KeePass Web 样式。配色与观感对齐 keeweb（github.com/keeweb/keeweb）：
   色值取自 app/styles/themes/_light.scss 与 _dark.scss，跟随系统 prefers-color-scheme 切换；
   列表为扁平行（细分隔线 + 选中蓝底白字），侧栏/弹层用 secondary 底色。
   历史坑：本文件曾因把 read_file 的 "N|" 行号前缀写回而整表失效——重写时勿带前缀。 */

:root {
  /* keeweb light */
  --bg: #f6f6f6;            /* background-color */
  --panel: #efefef;         /* secondary-background-color */
  --panel-2: #e4e4e4;       /* hover / 更一档 */
  --input-bg: #ffffff;
  --line: #dedede;          /* light-border-color */
  --fg: #303030;            /* text-color */
  --muted: #6c6c6c;
  --accent: #3063d4;        /* action-color */
  --sel: #2366d9;           /* selected-item-color */
  --sel-fg: #f6f6f6;        /* selected-item-text-color */
  --danger: #d04745;
  --warn: #b26a00;
  --ok: #2e7d32;
  --banner-error: #fdecea;
  --banner-info: #e8f0fe;
  --shadow: 0 10px 34px rgba(0, 0, 0, .18);
  --radius: 4px;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* keeweb dark */
    --bg: #1e1e1e;
    --panel: #2d2d2e;
    --panel-2: #3a3a3b;
    --input-bg: #26262b;
    --line: #444445;
    --fg: #f7f7f7;
    --muted: #b7b7b8;
    --accent: #317ef6;
    --sel: #2463c8;
    --sel-fg: #f6f6f6;
    --danger: #ec655a;
    --warn: #e8873a;
    --ok: #78b756;
    --banner-error: #3b1d1d;
    --banner-info: #1d2a3b;
    --shadow: 0 10px 34px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

/* 下面很多规则用 class / 元素选择器设了 display，会盖掉浏览器默认的
   [hidden] { display: none }（作者样式优先于 UA 样式），导致 hidden 属性完全失效：
   解锁后卡片不消失、banner 常驻成一条红条、锁定后工作区不收起。必须强制。 */
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 14px/1.5 -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--input-bg);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 6px 10px;
}

button { cursor: pointer; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
/* keeweb 的工具按钮是无边框扁平的，hover 才出底色 */
button.ghost { background: transparent; border-color: transparent; }
button.ghost:hover { background: var(--panel-2); border-color: transparent; }
button.small { padding: 2px 8px; font-size: 12px; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 25%, transparent);
}
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; margin: 12px 0 4px; color: var(--muted); font-size: 12px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.tight { margin: 0; }
input, select, textarea { width: 100%; }
/* 上面那条 width:100% 会把复选框拉成一块大板子，得单独收回来 */
input[type="checkbox"] { width: auto; flex: none; padding: 0; accent-color: var(--accent); }
.row > input { flex: 1; }
.row > input[type="checkbox"] { flex: none; }
.row > button { width: auto; flex: none; }
menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 16px 0 0; }

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 600; }
.brand em { color: var(--accent); font-style: normal; }
.db-name { font-weight: 600; }
.db-meta { color: var(--muted); font-size: 12px; }
.spacer { flex: 1; }
.idle { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.idle.warn { color: var(--warn); }
.whoami { font-size: 13px; font-weight: 600; }
header button { width: auto; flex: none; }
/* 顶栏身份分组：桌面是行内两个小组，移动端整体搬进抽屉后竖排 */
.db-ident, .id-ident { display: flex; align-items: center; gap: 12px; min-width: 0; }
.db-ident .db-name, .db-ident .db-meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ⋯ 下拉：桌面次级动作收这里，顶栏只留「锁定」常驻 */
.topmenu-wrap { position: relative; }
.topmenu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px;
  min-width: 130px;
  z-index: 30;
  box-shadow: var(--shadow);
}
.topmenu button { width: 100%; text-align: left; }
/* menuBtn 只由 JS 的 hidden 控制（断点搬家），CSS 不再二次设显示 */
#menuBtn { display: inline-flex; }

#banner {
  padding: 10px 16px;
  background: var(--banner-error);
  border-bottom: 1px solid var(--danger);
  color: var(--danger);
  display: flex;
  gap: 12px;
  align-items: center;
}
#banner.info { background: var(--banner-info); border-color: var(--accent); color: var(--fg); }

.centered { display: grid; place-items: center; min-height: 80vh; padding: 20px; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 24px;
  width: min(440px, 100%);
  box-shadow: var(--shadow);
}
.card h1 { margin: 0 0 8px; font-size: 20px; }
.hint, .fineprint { color: var(--muted); font-size: 12px; }
/* 同一个 .hint 既放说明文字也放失败提示，不给失败态换个颜色就分不出谁出错了。 */
.hint.error { color: var(--danger); }
.fineprint { margin: 14px 0 0; }
details { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 12px; }
summary { cursor: pointer; color: var(--muted); font-size: 13px; }
#unlockForm button, #createForm button, #authForm button, #bootstrapForm button,
#createUserForm button { margin-top: 14px; }
/* 卡片里同一时刻只有一个步骤可见（[hidden] 在上面那条规则里被强制了） */
#bootstrapStep h1, #authStep h1 { font-size: 20px; }

main {
  display: grid;
  grid-template-columns: 240px minmax(320px, 1fr) minmax(320px, 460px);
  gap: 1px;
  background: var(--line);
  min-height: calc(100vh - 53px);
  align-items: start;
}
main > * { background: var(--bg); padding: 14px; }
/* 分组侧栏 = keeweb 的 secondary 底色，和列表拉开一档 */
main > aside { background: var(--panel); }
.pane-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-bottom: 10px;
}

ul { list-style: none; margin: 0; padding: 0; }
.tree li { margin: 1px 0; }
.tree a {
  display: flex;
  gap: 6px;
  justify-content: space-between;
  padding: 6px 8px;
  border-radius: 3px;
  color: var(--fg);
  text-decoration: none;
  cursor: pointer;
}
.tree a:hover { background: var(--panel-2); }
.tree a.active { background: var(--sel); color: var(--sel-fg); }
.tree .count { color: var(--muted); font-size: 11px; }
.tree a.active .count { color: var(--sel-fg); opacity: .8; }
.tree ul { margin-left: 12px; border-left: 1px solid var(--line); padding-left: 4px; }

.searchbar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.searchbar input[type="search"] { flex: 1 1 220px; width: auto; }
.searchbar label { margin: 0; width: auto; }
.searchbar button { width: auto; flex: none; }

/* keeweb 风格：条目是扁平行 + 细线分隔，选中整行蓝底白字，而不是圆角卡片 */
#entryList li {
  border-bottom: 1px solid var(--line);
  padding: 10px 8px;
  cursor: pointer;
  background: transparent;
  display: flex;
  gap: 10px;
  align-items: baseline;
}
#entryList li:hover { background: var(--panel); }
#entryList li.active { background: var(--sel); color: var(--sel-fg); }
#entryList .title { font-weight: 600; }
#entryList .sub { color: var(--muted); font-size: 12px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#entryList li.active .sub { color: var(--sel-fg); opacity: .85; }
#entryList li.active .badge { border-color: rgba(255, 255, 255, .45); color: var(--sel-fg); }
#entryList li.active .qbtn { color: var(--sel-fg); background: transparent; border-color: rgba(255, 255, 255, .45); }
.badge { font-size: 10px; padding: 1px 6px; border-radius: 99px; border: 1px solid var(--line); color: var(--muted); }
.badge.expired { color: var(--danger); border-color: var(--danger); }
.badge.trashed { color: var(--warn); border-color: var(--warn); }
.badge.otp { color: var(--accent); border-color: var(--accent); }
.badge.att { color: var(--ok); border-color: var(--ok); }
.badge.ref { color: var(--warn); border-color: var(--warn); }
.badge.role-admin { color: var(--accent); border-color: var(--accent); }
.badge.role-user { color: var(--muted); }
.badge.disabled { color: var(--danger); border-color: var(--danger); }
.badge.warn { color: var(--warn); border-color: var(--warn); }

/* 用户与授权面板：一行一个账号，危险动作折进 details */
#userList li {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--panel);
}
#userList .head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#userList .name { font-weight: 600; }
#userList .grants { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 6px; color: var(--muted); font-size: 12px; }
#userList .chip {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 1px 4px 1px 8px;
  font-size: 12px;
  color: var(--fg);
  background: var(--input-bg);
}
#userList .grantline { margin-top: 6px; }
#userList select, #userList button { width: auto; }
#userList .section-title { margin: 12px 0 4px; }

.pager { display: flex; gap: 10px; align-items: center; justify-content: center; margin-top: 10px; color: var(--muted); font-size: 12px; }
.pager button { width: auto; }

/* 返回按钮只在移动翻页态出现；基座先藏掉，否则桌面三栏 grid 里它会被排到第二行 */
#backBtn { display: none; }
#detail h2 { margin: 0 0 4px; font-size: 18px; word-break: break-word; }
#detail.empty { color: var(--muted); display: grid; place-items: center; }
.field { display: grid; grid-template-columns: 92px 1fr auto; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.field .k { color: var(--muted); font-size: 12px; }
.field .v { word-break: break-all; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; }
.field .v.wrap { white-space: pre-wrap; font-family: inherit; }
.field .v.plain { font-family: inherit; }
.field button { width: auto; }
.secret.revealed { color: var(--warn); }
.totp-code { font-size: 22px; letter-spacing: .18em; font-weight: 600; color: var(--accent); }
.detail-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.detail-actions button { width: auto; }
.section-title { margin: 18px 0 6px; color: var(--muted); font-size: 12px; text-transform: uppercase; }
.muted { color: var(--muted); font-size: 12px; }

dialog {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 20px;
  width: min(520px, 92vw);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 8px; font-size: 18px; }
dialog details { margin-top: 12px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 10px; }
.checks label { margin: 0; width: auto; color: var(--fg); font-size: 13px; }

/* 强度条用原生 <meter> 而不是 div + style.width：本服务的 CSP 是 style-src 'self'，
   不靠内联样式才不会有被拦的风险。 */
.meter { display: flex; align-items: center; gap: 8px; margin: 6px 0 0; }
.meter meter { flex: 1; height: 8px; }
#pwMeterText { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
#pwMeterText.lv0 { color: var(--danger); }
#pwMeterText.lv1 { color: var(--warn); }
#pwMeterText.lv2 { color: var(--fg); }
#pwMeterText.lv3 { color: var(--ok); }
#pwMeterText.lv4 { color: var(--ok); }

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 14px;
  z-index: 20;
  box-shadow: var(--shadow);
}
.toast.error { border-color: var(--danger); color: var(--danger); }

/* 窄屏改成单栏堆叠；分组树在 ≤640px 会整体搬进抽屉（见下）。 */
@media (max-width: 1000px) {
  main { grid-template-columns: 1fr; }
  main > aside { border-bottom: 1px solid var(--line); }
}

/* 移动端外壳三件套：桌面断点与 JS 未接管（无 .chrome-ready）前一律不可见 */
#drawer, #drawerMask, #fabNew { display: none; }

/* ================= 移动端（≤640px）=================
 * 三栏堆叠在手机上等于"条目详情永远在屏幕外"，改为列表↔详情翻页：
 * JS 在选中条目时给 body 挂 .detail-open，CSS 负责收合/展开两个"页面"。
 * 分组树、次级动作、身份信息整体搬进右侧抽屉（MOBILE_SLOTS），原位的空容器
 * 由 .chrome-ready 门控收起，避免 JS 未接管时把内容隐没。 */
@media (max-width: 640px) {
  header { flex-wrap: wrap; gap: 6px 8px; padding: 8px 10px calc(8px + env(safe-area-inset-top)); }
  header button { min-height: 38px; }
  /* 搬家完成后：顶栏只留品牌 + 锁定 + ☰ */
  body.chrome-ready .topmenu-wrap { display: none; }

  main { display: block; min-height: calc(100dvh - 96px); }
  main > * { padding: 10px; }
  body.chrome-ready main > aside { display: none; }

  /* 列表↔详情翻页：backBtn 平时不存在于布局，进详情页才亮出 */
  body:not(.detail-open) #detail { display: none; }
  body.detail-open aside, body.detail-open #listPane { display: none; }
  #backBtn { display: none; }
  body.detail-open #backBtn { display: block; margin-bottom: 10px; }
  .back-btn { min-height: 40px; width: auto; margin: 0 0 10px; }
  .field { grid-template-columns: 76px 1fr auto; }
  .detail-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .detail-actions button { min-height: 44px; width: 100%; }

  /* 搜索条：输入框整行；新建入口交给 FAB（搬家后隐藏原按钮） */
  .searchbar { gap: 6px; }
  .searchbar input[type="search"] { flex-basis: 100%; }
  .searchbar button { min-height: 44px; }
  body.chrome-ready #newEntryBtn { display: none; }
  #entryList li { padding: 12px 8px; }
  .pager button { min-height: 42px; }

  /* 抽屉：分组树 + 过滤开关 + 次级动作 + 身份信息 */
  body.chrome-ready #drawer {
    display: flex; flex-direction: column; gap: 12px;
    position: fixed; top: 0; right: 0; bottom: 0; width: min(320px, 86vw);
    background: var(--panel); border-left: 1px solid var(--line);
    padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
    overflow: auto; z-index: 75;
    transform: translateX(105%); transition: transform .18s ease;
  }
  body.chrome-ready #drawer.open { transform: translateX(0); }
  .drawer-head { display: flex; align-items: center; justify-content: space-between; }
  .drawer-info { color: var(--muted); font-size: 12px; display: flex; flex-direction: column; gap: 2px; }
  #drawer .tree a { padding: 10px 8px; }
  #drawer button { min-height: 42px; }
  #drawerActions { display: flex; flex-wrap: wrap; gap: 8px; }
  #drawerActions button { flex: 1; min-width: 90px; }
  #drawerFilters { display: flex; flex-direction: column; gap: 10px; color: var(--fg); font-size: 14px; }
  #drawerMask.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 70; }

  /* 悬浮新建：只在工作区可见时出现 */
  body.chrome-ready.in-workspace #fabNew {
    display: grid; place-items: center;
    position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
    width: 54px; height: 54px; border-radius: 50%; border: none;
    font-size: 26px; line-height: 1; z-index: 60;
    box-shadow: 0 6px 18px rgba(0,0,0,.35);
  }

  /* 弹层全屏页化。尺寸一律由 inset:0 撑开，不写 100vw/100dvh：
     有滚动条时 vw 含滚动条宽度，dialog 会比视口宽出十几像素并把内容顶偏。 */
  dialog { position: fixed; inset: 0; width: auto; max-width: none; height: auto;
    max-height: none; margin: 0; border: none; border-radius: 0; overflow: auto;
    padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); }
  dialog menu { position: sticky; bottom: 0; background: var(--bg);
    margin: 16px -14px -14px; padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line); }
  dialog menu button { min-height: 46px; }

  /* 「更多设置」折叠区：整行可点、目标够大 */
  details.more > summary { min-height: 44px; display: flex; align-items: center;
    border: 1px solid var(--line); border-radius: 3px; padding: 8px 10px; background: var(--panel); }

  /* 触摸友好的表单尺寸（复选框保持原形） */
  input:not([type="checkbox"]), select, textarea { min-height: 42px; font-size: 15px; }
  button { min-height: 38px; }
  .card { padding: 18px; }
  .toast { left: 12px; right: 12px; bottom: 12px; z-index: 90; }
}

/* 列表行内快捷复制按钮：桌面与移动同值（触摸目标在移动断点已全局放大） */
.qbtn { flex: none; min-width: 30px; }

/* 数据库管理弹窗行 */
.dbrow { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.dbrow:last-of-type { border-bottom: none; }
.dbinfo { flex: 1; min-width: 220px; }
.dbrow .row { gap: 6px; }

/* 分组树拖拽与 hover 动作 */
.tree a { position: relative; }
.gacts { display: none; gap: 2px; margin-left: auto; }
.tree li:hover > a > .gacts { display: inline-flex; }
.gact { padding: 0 6px; font-size: 12px; line-height: 18px; border: 1px solid var(--line); background: transparent; color: var(--muted, #888); border-radius: 4px; cursor: pointer; }
.gact:hover { color: var(--accent); border-color: var(--accent); }
#groupTree li[draggable="true"] { cursor: grab; }
#groupTree li.dragging { opacity: 0.45; }
#groupTree li.drop-hl > a { background: color-mix(in srgb, var(--accent) 18%, transparent); outline: 1px dashed var(--accent); outline-offset: -1px; }
#entryList li.dragging { opacity: 0.5; }
#entryList li { cursor: grab; }

/* 分组树折叠三角 */
.tree .caret { display:inline-block; width:14px; flex:none; text-align:center; color:var(--muted,#8a94a6); cursor:pointer; font-size:11px; user-select:none; }
.tree .caret:hover { color:var(--accent,#6ea8fe); }
.tree .caret-none { cursor:default; }
