/* 室友记账 —— 移动优先，跟随系统深浅色，不引任何外部资源。
   颜色 token 取自已验证的数据可视化调色板（浅色面 #fcfcfb / 深色面 #1a1a19）。 */

:root {
  color-scheme: light;

  --surface-0: #f4f3f0;      /* 页面底色 */
  --surface-1: #fcfcfb;      /* 卡片 */
  --surface-2: #eceae5;      /* 输入框、次级块 */
  --surface-3: #e2e0d9;

  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #7c7a73;

  --border:        #e0ded7;
  --border-strong: #c9c6bd;

  --accent:      #2a78d6;    /* 分类槽位 1 */
  --accent-soft: #e8f0fc;
  --accent-text: #ffffff;

  --positive: #1baf7a;       /* 别人欠你 */
  --negative: #e34948;       /* 你欠别人 */
  --warn:     #eda100;

  --radius-sm: 8px;
  --radius:    14px;
  --radius-lg: 20px;

  --shadow: 0 1px 2px rgba(11, 11, 11, .05), 0 4px 16px rgba(11, 11, 11, .04);
  --shadow-lg: 0 4px 12px rgba(11, 11, 11, .08), 0 16px 40px rgba(11, 11, 11, .1);

  --tab-h: 62px;

  /* iPhone 底部 Home 指示条的安全区。抽成变量有两个原因：
     一是下面几处要用同一个值，二是测试时能覆盖它模拟真机。 */
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --surface-3: #302f2c;

    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #8d8b81;

    --border:        #302f2c;
    --border-strong: #45443f;

    --accent:      #3987e5;
    --accent-soft: #17293f;

    --positive: #199e70;
    --negative: #e66767;
    --warn:     #c98500;

    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    --shadow-lg: 0 4px 12px rgba(0, 0, 0, .4), 0 16px 40px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

/* 任何设了 display 的元素都会盖掉浏览器默认的 [hidden]{display:none}，
   于是 hidden 属性形同虚设。踩过两次（月份按钮、底部导航栏），
   这里一条规则根治，别再逐个补。 */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--tab-h) + var(--safe-bottom));
}

/* 数字统一用等宽变体对齐，金额列不会跳 */
.num { font-variant-numeric: tabular-nums; }

/* ── 布局 ────────────────────────────────────────────────── */

.app { max-width: 720px; margin: 0 auto; padding: 0 16px; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 0 10px;
  background: color-mix(in srgb, var(--surface-0) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.topbar h1 {
  font-size: 17px; font-weight: 650; margin: 0; letter-spacing: -.01em;
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.view { display: none; animation: fade .18s ease; }
.view.active { display: block; }

@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  .view, .sheet, .toast { animation: none !important; }
  * { transition: none !important; }
}

/* ── 卡片 ────────────────────────────────────────────────── */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.card-title {
  font-size: 12px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-muted);
  margin: 0 0 12px;
}

/* ── 头条数字 ────────────────────────────────────────────── */

.hero { text-align: center; padding: 26px 16px 22px; }
.hero-label { font-size: 14px; color: var(--text-secondary); margin-bottom: 6px; }
.hero-amount {
  font-size: clamp(38px, 12vw, 52px);
  font-weight: 700; letter-spacing: -.03em; line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.hero-amount.positive { color: var(--positive); }
.hero-amount.negative { color: var(--negative); }
.hero-amount.zero     { color: var(--text-primary); }
.hero-sub { font-size: 13px; color: var(--text-muted); margin-top: 8px; }

/* ── 统计小块 ────────────────────────────────────────────── */

.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat {
  background: var(--surface-2); border-radius: var(--radius-sm);
  padding: 12px 14px;
}
.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 3px; }
.stat-value { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }

/* ── 分布条形图 ──────────────────────────────────────────── */
/* 一个系列 = 一个颜色。类别身份由 emoji + 文字承担，不用色相区分。 */

.bars { display: flex; flex-direction: column; gap: 11px; }
.bar-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: baseline; }
.bar-name { font-size: 14px; display: flex; align-items: center; gap: 7px; min-width: 0; }
.bar-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-value { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.bar-pct { font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.bar-track {
  grid-column: 1 / -1; height: 8px; background: var(--surface-2);
  border-radius: 4px; overflow: hidden;
}
.bar-fill {
  height: 100%; background: var(--accent);
  border-radius: 4px;                 /* 4px 圆角的数据端 */
  transition: width .35s cubic-bezier(.4, 0, .2, 1);
}

/* 趋势迷你柱图 */
.trend { display: flex; align-items: flex-end; gap: 6px; height: 64px; margin-bottom: 8px; }
.trend-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; }
.trend-bar { background: var(--accent); border-radius: 4px 4px 0 0; min-height: 3px; }
.trend-bar.dim { background: var(--border-strong); }
.trend-labels { display: flex; gap: 6px; }
.trend-labels div { flex: 1; text-align: center; font-size: 10px; color: var(--text-muted); }

/* ── 列表 ────────────────────────────────────────────────── */

.month-head {
  font-size: 12px; font-weight: 600; color: var(--text-muted);
  letter-spacing: .05em; margin: 18px 2px 8px;
  display: flex; justify-content: space-between;
}

.item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 8px; cursor: pointer;
  transition: border-color .15s, transform .1s;
  width: 100%; text-align: left; font: inherit; color: inherit;
}
.item:hover { border-color: var(--border-strong); }
.item:active { transform: scale(.994); }

.item-icon {
  width: 40px; height: 40px; flex: none;
  display: grid; place-items: center; font-size: 19px;
  background: var(--surface-2); border-radius: 11px;
}
.item-main { flex: 1; min-width: 0; }
.item-title {
  font-size: 15px; font-weight: 550;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item-sub {
  font-size: 12.5px; color: var(--text-muted); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item-amount { font-size: 16px; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.item-right { text-align: right; flex: none; }

.avatar {
  display: inline-grid; place-items: center;
  width: 20px; height: 20px; border-radius: 50%;
  font-size: 11px; color: #fff; flex: none;
}

.tag {
  display: inline-block; font-size: 11px; padding: 1px 6px;
  border-radius: 5px; background: var(--surface-2); color: var(--text-muted);
  vertical-align: middle;
}
.tag.ai { background: var(--accent-soft); color: var(--accent); }

/* ── 表单 ────────────────────────────────────────────────── */

label.field { display: block; margin-bottom: 14px; }
.field-label { font-size: 13px; font-weight: 550; color: var(--text-secondary); margin-bottom: 6px; display: block; }

input[type="text"], input[type="number"], input[type="date"],
input[type="password"], textarea, select {
  width: 100%;
  /* 网格/弹性项默认 min-width:auto，不肯缩到内容宽度以下。
     input[type=date] 的原生控件固有宽度很大（iOS 上更宽），
     不写这行的话它会霸占整行，把旁边的框挤扁甚至挤重叠。 */
  min-width: 0;
  padding: 11px 13px; font: inherit; font-size: 16px; /* 16px 防止 iOS 聚焦时缩放 */
  color: var(--text-primary); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
textarea { resize: vertical; min-height: 100px; line-height: 1.55; }

.field.flagged input, .field.flagged select {
  border-color: var(--warn);
  background: color-mix(in srgb, var(--warn) 8%, var(--surface-2));
}
.flag-note { font-size: 12px; color: var(--warn); margin-top: 4px; }

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row2 > * { min-width: 0; }

/* 窄屏上两栏挤不下就摞成一列。
   注意 .row2 现在只用于 select / number 这类宽度可控的控件 ——
   input[type=date] 已经单独占一行了，因为 iOS Safari 的原生日期控件
   比 Chromium 宽很多，塞进两栏必然重叠，而且这个差异在桌面浏览器上测不出来。 */
@media (max-width: 420px) {
  .row2 { grid-template-columns: 1fr; gap: 0; }
}

/* 日期控件在深色模式下的原生日历图标默认是黑的，几乎看不见 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) input[type="date"]::-webkit-calendar-picker-indicator {
    filter: invert(1) opacity(.65);
  }
}

/* ── 按钮 ────────────────────────────────────────────────── */

button { font: inherit; cursor: pointer; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 12px 18px; font-size: 15px; font-weight: 600;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text-primary);
  transition: opacity .15s, transform .1s, background .15s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-ghost { background: transparent; border-color: var(--border-strong); }
.btn-danger { background: transparent; border-color: var(--border-strong); color: var(--negative); }
.btn-block { width: 100%; }
.btn-sm { padding: 7px 12px; font-size: 13px; }

.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

/* 选择器（谁付的 / 类别 / 分账方式） */

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

/* 明细页的筛选：类别和付款人各自一个框，各有标题，
   放一个框里两排 chip 挤在一起看不出谁是谁。 */
.filter-card { padding: 12px 14px; margin-bottom: 10px; }
.filter-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 9px; min-height: 22px;
}
.filter-clear { padding: 3px 9px; font-size: 12px; font-weight: 500; }
.chip {
  padding: 8px 13px; font-size: 14px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text-secondary); transition: all .15s;
}
.chip[aria-pressed="true"] {
  background: var(--accent); border-color: var(--accent);
  color: var(--accent-text); font-weight: 600;
}
.chip.cat { border-radius: var(--radius-sm); }

/* ── 底部导航 ────────────────────────────────────────────── */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex;
  /* box-sizing 是 border-box，所以安全区必须加进 height，
     否则会从 62px 里扣掉，图标和文字被压扁 —— 浏览器里看不出来，
     一装到主屏幕（standalone）就露馅。 */
  height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--surface-1) 92%, transparent);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px;
  background: none; border: none; color: var(--text-secondary);
  font-size: 11px; font-weight: 500; padding: 0;
  transition: color .15s;
  -webkit-tap-highlight-color: transparent;
}
.tab svg { width: 21px; height: 21px; stroke-width: 1.9; }
.tab[aria-selected="true"] { color: var(--accent); font-weight: 600; }
.tab-add svg { width: 27px; height: 27px; }

/* ── 弹层 ────────────────────────────────────────────────── */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, .45);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease;
}
@media (min-width: 640px) { .sheet-backdrop { align-items: center; } }

.sheet {
  background: var(--surface-1); width: 100%; max-width: 560px;
  max-height: 88vh; overflow-y: auto;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 20px 18px calc(20px + var(--safe-bottom));
  box-shadow: var(--shadow-lg);
  animation: slideup .22s cubic-bezier(.32, .72, 0, 1);
}
@media (min-width: 640px) { .sheet { border-radius: var(--radius-lg); max-height: 84vh; } }
@keyframes slideup { from { transform: translateY(100%); } to { transform: none; } }

.sheet h2 { font-size: 18px; font-weight: 650; margin: 0 0 16px; letter-spacing: -.01em; }
.sheet-grabber {
  width: 36px; height: 4px; border-radius: 2px; background: var(--border-strong);
  margin: -6px auto 14px;
}

/* ── 记一笔的三个入口 ────────────────────────────────────── */

.segmented {
  display: flex; gap: 3px; padding: 3px; margin-bottom: 16px;
  background: var(--surface-2); border-radius: 10px;
}
.segmented button {
  flex: 1; padding: 9px 6px; font-size: 14px; font-weight: 550;
  border: none; border-radius: 7px; background: none; color: var(--text-secondary);
  transition: all .15s;
}
.segmented button[aria-selected="true"] {
  background: var(--surface-1); color: var(--text-primary);
  box-shadow: var(--shadow); font-weight: 600;
}

.dropzone {
  border: 2px dashed var(--border-strong); border-radius: var(--radius);
  padding: 34px 20px; text-align: center; color: var(--text-muted);
  transition: all .18s; cursor: pointer; background: var(--surface-2);
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone-icon { font-size: 32px; margin-bottom: 8px; }
.dropzone-main { font-size: 15px; font-weight: 550; color: var(--text-primary); margin-bottom: 3px; }

.preview { position: relative; margin-bottom: 14px; }
.preview img {
  width: 100%; max-height: 260px; object-fit: contain;
  border-radius: var(--radius); background: var(--surface-2);
  border: 1px solid var(--border);
}

/* ── 状态 ────────────────────────────────────────────────── */

.spinner {
  width: 17px; height: 17px; border-radius: 50%;
  border: 2px solid color-mix(in srgb, currentColor 25%, transparent);
  border-top-color: currentColor;
  animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.thinking {
  display: flex; align-items: center; gap: 11px; justify-content: center;
  padding: 30px 16px; color: var(--text-secondary); font-size: 14px;
}

.empty { text-align: center; padding: 44px 20px; color: var(--text-muted); }
.empty-icon { font-size: 40px; margin-bottom: 12px; opacity: .55; }
.empty-title { font-size: 15px; font-weight: 550; color: var(--text-secondary); margin-bottom: 4px; }

.banner {
  padding: 11px 13px; border-radius: var(--radius-sm); font-size: 13.5px;
  margin-bottom: 14px; line-height: 1.5;
}
.banner-warn  { background: color-mix(in srgb, var(--warn) 14%, var(--surface-1)); color: var(--text-primary); border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); }
.banner-error { background: color-mix(in srgb, var(--negative) 12%, var(--surface-1)); color: var(--text-primary); border: 1px solid color-mix(in srgb, var(--negative) 35%, transparent); }
.banner-info  { background: var(--accent-soft); color: var(--text-primary); border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-bottom) + 18px);
  transform: translateX(-50%); z-index: 60;
  background: var(--text-primary); color: var(--surface-1);
  padding: 11px 18px; border-radius: 999px; font-size: 14px; font-weight: 550;
  box-shadow: var(--shadow-lg); animation: fade .18s ease;
  max-width: calc(100vw - 32px); text-align: center;
}

/* ── 结算 ────────────────────────────────────────────────── */

.settle-card {
  display: flex; align-items: center; gap: 14px;
  padding: 16px; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px;
}
.settle-arrow { font-size: 19px; color: var(--text-muted); }
.settle-who { font-size: 15px; font-weight: 600; }
.settle-amount { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; margin-left: auto; }

.balance-row {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 0; border-bottom: 1px solid var(--border);
}
.balance-row:last-child { border-bottom: none; }
.balance-net { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }
.balance-net.positive { color: var(--positive); }
.balance-net.negative { color: var(--negative); }

/* ── 杂项 ────────────────────────────────────────────────── */

.muted { color: var(--text-muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.mt { margin-top: 14px; }
.hr { height: 1px; background: var(--border); border: none; margin: 16px 0; }

.split-detail { display: flex; flex-direction: column; gap: 9px; margin-top: 10px; }
.split-detail-row { display: grid; grid-template-columns: 1fr 120px; gap: 10px; align-items: center; }
.split-preview { font-size: 13px; color: var(--text-muted); margin-top: 8px; line-height: 1.6; }

table.plain { width: 100%; border-collapse: collapse; font-size: 14px; }
table.plain th {
  text-align: left; font-size: 11px; letter-spacing: .05em; text-transform: uppercase;
  color: var(--text-muted); font-weight: 600; padding: 0 8px 8px 0;
}
table.plain td { padding: 7px 8px 7px 0; border-top: 1px solid var(--border); }
table.plain td.num { text-align: right; padding-right: 0; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* ── 离线状态 ────────────────────────────────────────────── */

/* 离线提示条。用文档流里的真实元素，不用 ::after 叠加 ——
   叠加会盖住顶栏的标题和按钮，实测踩过。 */
.offline-bar {
  background: var(--warn);
  color: #1a1a19;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  margin: -6px 0 14px;
  letter-spacing: .01em;
  line-height: 1.4;
}

/* 离线时联网才有用的 tab 弱化掉，但不禁用 —— 点进去有解释 */
body.is-offline .tab:not([data-view="add"]):not([aria-selected="true"]) { opacity: .45; }

/* 「记一笔」上的待同步角标 */
.tab { position: relative; }
.tab.has-pending::after {
  content: "";
  position: absolute;
  top: 8px; right: calc(50% - 16px);
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--warn);
  border: 2px solid var(--surface-1);
}

/* ── 登录 / 注册 / 邀请：整页接管 ────────────────────────── */

.gate-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 26px 22px;
  margin: 40px auto 24px;
  max-width: 420px;
  box-shadow: var(--shadow-lg);
}
.gate-card h2 { font-size: 21px; font-weight: 700; margin: 0 0 10px; letter-spacing: -.01em; }
.gate-brand { font-size: 44px; text-align: center; margin-bottom: 10px; }

@media (max-width: 420px) {
  .gate-card { margin-top: 20px; padding: 22px 18px; border-radius: var(--radius); }
}

/* ── 账本切换 ────────────────────────────────────────────── */

#btn-ledger {
  max-width: 46vw;
  display: inline-flex; align-items: center; gap: 5px;
  overflow: hidden;
}
#btn-ledger .ledger-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* 切换列表里的一行：图标 + 名字 + 副标题 */
.ledger-row {
  width: 100%;
  display: flex; align-items: center; gap: 11px;
  padding: 12px 14px;
  text-align: left;
  line-height: 1.35;
}

/* ── 邀请链接 ────────────────────────────────────────────── */

.invite-link {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  word-break: break-all;
  user-select: all;             /* 点一下就能全选，方便手动复制 */
  line-height: 1.5;
}
