:root {
  --bg: #f4f5f8;
  --card: #ffffff;
  --ink: #18202e;
  --muted: #667085;
  --line: #e3e6ec;
  --brand: #1f3a68;
  --brand-2: #2c5299;
  --ok: #157a4a;
  --ok-bg: #e5f5ec;
  --warn: #b25e00;
  --warn-bg: #fff2df;
  --bad: #b42318;
  --bad-bg: #fde8e7;
  --radius: 14px;
  --tabbar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c; --card: #1a2029; --ink: #e8ebf0; --muted: #98a2b3; --line: #2b3340;
    --brand: #5b8def; --brand-2: #7aa5ff; --ok: #47c486; --ok-bg: #12301f; --warn: #f5a524; --warn-bg: #36270d;
    --bad: #f97066; --bad-bg: #3b1715;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
body.auth { padding-bottom: 0; }
a { color: var(--brand-2); }
h1 { font-size: 1.5rem; margin: .4rem 0 .8rem; }
h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
h2.section { margin: 1.4rem 0 .6rem; display: flex; gap: .5rem; align-items: baseline; }
.muted { color: var(--muted); font-weight: normal; font-size: .92em; }
.hint { color: var(--muted); font-size: .85rem; margin: .2rem 0 .4rem; }
.warn { color: var(--warn); }
.ok { color: var(--ok); }
.pre { white-space: pre-line; margin: .2rem 0; }
.upper { text-transform: uppercase; }

.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--brand); color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
}
.brand { color: #fff; text-decoration: none; font-weight: 700; font-style: italic; font-family: Georgia, "Times New Roman", serif; font-size: 1.2rem; }
.top-btn { color: #fff; text-decoration: none; font-weight: 600; padding: 6px 12px; border: 1px solid rgba(255,255,255,.5); border-radius: 999px; }

.wrap { max-width: 720px; margin: 0 auto; padding: 12px 16px; }

.flash { background: var(--ok-bg); color: var(--ok); padding: 12px 14px; border-radius: 10px; margin-bottom: 12px; font-weight: 600; }
.flash.error { background: var(--bad-bg); color: var(--bad); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }

label { display: block; font-size: .9rem; color: var(--muted); margin-bottom: 10px; }
input[type=text], input[type=tel], input[type=email], input[type=password], input[type=date], input[type=search], input:not([type]), select, textarea {
  display: block; width: 100%; margin-top: 4px; font: inherit; color: var(--ink);
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; min-height: 48px;
}
textarea { resize: vertical; line-height: 1.35; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: 0; border-color: transparent; }
label.check { display: flex; gap: 10px; align-items: center; color: var(--ink); font-size: .95rem; }
label.check input { width: 22px; height: 22px; flex: none; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: .8fr 1fr 1fr; gap: 10px; }
@media (max-width: 380px) { .grid2 { grid-template-columns: 1fr; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; width: 100%;
  min-height: 48px; padding: 10px 16px; margin: 4px 0 10px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.ghost { background: transparent; }
.btn.big { min-height: 56px; font-size: 1.1rem; }
.btn.huge { min-height: 72px; font-size: 1.35rem; border-radius: 16px; box-shadow: 0 6px 18px rgba(31,58,104,.25); }
.btn.small { width: auto; min-height: 40px; padding: 6px 12px; font-size: .9rem; margin: 0; }
.btn:disabled { opacity: .6; }
.icon-btn { border: 0; background: transparent; color: var(--muted); font-size: 1.2rem; min-width: 44px; min-height: 44px; cursor: pointer; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; text-decoration: none; color: inherit; }
.stat .k { color: var(--muted); font-size: .8rem; }
.stat .v { font-size: 1.15rem; font-weight: 700; margin-top: 2px; word-break: break-all; }
.stat .s { color: var(--muted); font-size: .78rem; }

.list { list-style: none; margin: 0 0 12px; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; color: inherit; text-decoration: none; }
.row .main { flex: 1; min-width: 0; color: inherit; text-decoration: none; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { color: var(--muted); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub.clamp { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.row .side { text-align: right; flex: none; }
.amt { font-weight: 700; }
.strike { text-decoration: line-through; color: var(--muted); }
.menu .row span { color: var(--muted); font-size: 1.3rem; }

.pill { display: inline-block; font-size: .75rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; margin-top: 2px; }
.pill.big { font-size: .9rem; padding: 6px 12px; }
.pill.paid { background: var(--ok-bg); color: var(--ok); }
.pill.unpaid { background: var(--warn-bg); color: var(--warn); }
.pill.cancelled { background: var(--bad-bg); color: var(--bad); }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.filters input[type=search] { grid-column: 1 / -1; }
.filters select, .filters input { margin-top: 0; }
.total-line { margin: 4px 2px 8px; }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; }

.item { border: 1px dashed var(--line); border-radius: 12px; padding: 10px; margin-bottom: 10px; }
.item-head { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; }
.item-head select { margin-top: 0; }
.line-amt { text-align: right; color: var(--muted); }
.line-amt b { color: var(--ink); }
.more-fields { margin: -2px 0 6px; color: var(--muted); font-size: .9rem; }
.more-fields summary { cursor: pointer; padding: 4px 0; }

.summary .sumrow, .sumrow { display: flex; justify-content: space-between; padding: 6px 0; }
.sumrow.total { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 10px; font-size: 1.2rem; }
.words { color: var(--muted); font-style: italic; font-size: .9rem; margin-top: 4px; }

.sticky-actions {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); z-index: 5;
  background: linear-gradient(to top, var(--bg) 70%, transparent); padding: 10px 0 2px;
}

.bill-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 10px; }
.bill-head h1 { margin: 0; }
.share-actions { margin-bottom: 8px; }
.view-item { border-bottom: 1px solid var(--line); padding: 6px 0 10px; margin-bottom: 6px; }
.view-item-amt { display: flex; justify-content: space-between; }
.inline-form { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.inline-form label { flex: 1; margin: 0; min-width: 150px; }
.inline-form .btn { width: auto; margin: 0; }

.danger-zone { margin: 16px 0; color: var(--muted); }
.danger-zone summary { cursor: pointer; padding: 8px 0; }

.sig-preview { max-width: 240px; max-height: 100px; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px; display: block; margin-bottom: 10px; }
.upload input[type=file] { margin-bottom: 8px; font: inherit; max-width: 100%; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; height: var(--tabbar-h);
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.tabbar .ic { font-size: 1.35rem; line-height: 1.1; }
.tabbar a.on { color: var(--brand-2); }
.tabbar a.new .ic { background: var(--brand); color: #fff; border-radius: 999px; width: 36px; height: 36px; display: grid; place-items: center; font-size: 1.2rem; }

.auth-card { max-width: 400px; margin: 12vh auto 0; background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 24px 20px; }
.auth-card h1 { font-family: Georgia, "Times New Roman", serif; font-style: italic; margin: 0; }
.auth-card .muted { margin: 2px 0 18px; }
