:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --sidebar: #15202b;
  --sidebar-2: #1c2b38;
  --sidebar-text: #b7c2cc;
  --sidebar-active: #ffffff;
  --line: #e4e9f0;
  --text: #15202b;
  --muted: #667085;
  --accent: #2f6fed;
  --accent-hover: #1f5ad9;
  --accent-soft: #e8effd;
  --member-accent: #0f766e;
  --member-hover: #0b5f58;
  --member-soft: #e6f4f2;
  --success: #0e9f6e;
  --success-bg: #e6f6ef;
  --danger: #d14343;
  --danger-bg: #fdecec;
  --warning: #b45309;
  --warning-bg: #fef3c7;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .04);
  --radius: 12px;
  --font: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  --top: 56px;
  --side: 232px;
  --bottom-nav-h: 72px;
  --main-bottom-extra: 56px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  min-height: 100vh;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; }
h1, h2, h3 { margin: 0 0 .4em; letter-spacing: -.02em; }
p { margin: 0 0 1em; }

/* Landing */
.landing { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 32px 16px; }
.landing-inner { width: min(860px, 100%); }
.landing h1 { font-size: 28px; }
.landing .sub { color: var(--muted); margin-bottom: 28px; }
.landing-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.landing-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: 28px; box-shadow: var(--shadow); display: block; color: inherit;
}
.landing-card:hover { text-decoration: none; border-color: #c9d4e4; transform: translateY(-1px); }
.landing-card h2 { font-size: 20px; }
.landing-card p { color: var(--muted); margin: 8px 0 16px; }
.badge-phase { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* Login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 32px 28px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 22px; }
.login-card .hint { color: var(--muted); font-size: 13px; margin-bottom: 20px; }
.login-card.member-theme { border-top: 4px solid var(--member-accent); }
.login-card.admin-theme { border-top: 4px solid var(--accent); }

/* App shell */
.app { display: none; min-height: 100vh; }
.app.show { display: block; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--side); background: var(--sidebar); color: var(--sidebar-text);
  padding: 18px 12px; z-index: 30; display: flex; flex-direction: column;
}
.brand { color: #fff; font-weight: 700; padding: 8px 12px 18px; font-size: 15px; letter-spacing: .02em; }
.brand small { display: block; font-weight: 400; color: var(--sidebar-text); font-size: 12px; margin-top: 4px; }
.nav a, .nav button.nav-link {
  display: flex; align-items: center; gap: 8px; width: 100%;
  color: var(--sidebar-text); background: none; border: 0; border-radius: 8px;
  padding: 10px 12px; text-align: left; cursor: pointer; text-decoration: none; font-size: 14px;
}
.nav a:hover, .nav button.nav-link:hover { background: var(--sidebar-2); color: #fff; text-decoration: none; }
.nav a.active, .nav button.nav-link.active { background: #2f6fed; color: #fff; }
body.portal-member .nav a.active, body.portal-member .nav button.nav-link.active { background: var(--member-accent); }
.sidebar .spacer { flex: 1; }
.side-user { padding: 12px; font-size: 12px; color: #8b98a5; }
.side-user strong { color: #fff; display: block; font-size: 13px; }
.topbar {
  position: sticky; top: 0; height: var(--top); background: rgba(255,255,255,.9); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px;
  padding: 0 20px 0 calc(var(--side) + 20px); z-index: 20;
}
.topbar h2 { font-size: 16px; font-weight: 650; margin: 0; flex: 1; }
.menu-btn { display: none; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 6px 10px; cursor: pointer; }
.main { padding: 20px 24px 48px calc(var(--side) + 24px); }

/* Forms & controls */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field .help { font-size: 12px; color: var(--muted); margin-top: 4px; font-weight: 400; }
input[type=text], input[type=password], input[type=number], input[type=url], select, textarea {
  width: 100%; border: 1px solid #d0d7e2; border-radius: 10px; padding: 9px 11px; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #cfe0ff; border-color: var(--accent); }
textarea { min-height: 80px; resize: vertical; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; align-items: end; }
.filters .field { margin: 0; min-width: 140px; flex: 1; }
.filters .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 10px; padding: 8px 14px; font-weight: 600; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 14px;
}
.btn:hover { background: var(--accent-hover); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
body.portal-member .btn.primary, .btn.member { background: var(--member-accent); }
body.portal-member .btn.primary:hover, .btn.member:hover { background: var(--member-hover); }
.btn.ghost { background: #fff; color: var(--text); border: 1px solid var(--line); }
.btn.ghost:hover { background: #f6f8fb; }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background: #b53535; }
.btn.success { background: var(--success); }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.linkish { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font-weight: 600; }

/* Cards / tables */
.grid-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--muted); }
.stat .value { font-size: 26px; font-weight: 700; letter-spacing: -.03em; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: auto; }
.panel h3, .section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line); font-size: 15px; margin: 0; }
.section { margin-bottom: 18px; }
.algo {
  background: #f8fafc; border: 1px dashed #d0d7e2; border-radius: 12px; padding: 14px 16px;
  color: #3f4b5a; font-size: 13px; white-space: pre-line; margin-bottom: 16px;
}
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 10px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfc; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #f8fafc; }
.code-cell { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 13px; letter-spacing: .02em; }
.pager { display: flex; align-items: center; gap: 10px; justify-content: flex-end; padding: 10px 14px; color: var(--muted); font-size: 13px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }

.badge { display: inline-block; border-radius: 999px; padding: 2px 8px; font-size: 12px; font-weight: 600; }
.badge.on, .badge.active, .badge.unused { background: var(--accent-soft); color: var(--accent); }
.badge.off, .badge.disabled, .badge.void { background: #eef0f3; color: #5d6a78; }
.badge.used, .badge.ok { background: var(--success-bg); color: var(--success); }
.badge.expired, .badge.warn { background: var(--warning-bg); color: var(--warning); }
.badge.self { background: #eef2ff; color: #3730a3; }

/* Toasts: success and error MUST look different */
.toast-wrap { position: fixed; top: 16px; right: 16px; z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast {
  min-width: 240px; max-width: min(420px, calc(100vw - 32px));
  padding: 11px 14px; border-radius: 10px; color: #fff; font-size: 14px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(16,24,40,.16); animation: in .18s ease;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.info { background: #334155; }
@keyframes in { from { transform: translateY(-6px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 70; display: flex; align-items: flex-start; justify-content: center; padding: 8vh 16px 16px; overflow: auto; }
.modal { width: min(560px, 100%); background: #fff; border-radius: 16px; box-shadow: 0 16px 48px rgba(15,23,42,.2); }
.modal header { padding: 16px 18px 0; font-weight: 700; }
.modal .body { padding: 16px 18px; }
.modal footer { padding: 0 18px 18px; display: flex; justify-content: flex-end; gap: 8px; }

.code-result { background: #0f172a; color: #e2e8f0; border-radius: 10px; padding: 12px; font-family: ui-monospace, Consolas, monospace; font-size: 14px; max-height: 240px; overflow: auto; white-space: pre-wrap; }

.muted { color: var(--muted); }
.hidden { display: none !important; }
.ml { margin-left: auto; }

.bottom-nav { display: none; }

@media (max-width: 960px) {
  .grid-cards { grid-template-columns: 1fr 1fr; }
  .row-3 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 800px) {
  .sidebar { transform: translateX(-110%); transition: transform .2s ease; }
  .sidebar.open { transform: none; }
  .topbar, .main { padding-left: 16px; padding-right: 16px; }
  .menu-btn { display: inline-flex; }
  .grid-cards, .landing-grid, .row-2, .row-3 { grid-template-columns: 1fr; }
  /* 底栏留白见下方统一规则，避免 84px 不够或 calc(env()) 失效时回退到过小值 */
  body.portal-member .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    background: #fff; border-top: 1px solid var(--line);
  }
  body.portal-member .bottom-nav a {
    flex: 1; text-align: center; padding: 10px 6px 12px; color: var(--muted);
    text-decoration: none; font-size: 12px; font-weight: 650;
  }
  body.portal-member .bottom-nav a.active { color: var(--member-accent); }
  .backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 25; }
}


/* Share fallback + stacked code actions */
.share-fallback {
  width: 100%;
  min-height: 180px;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 14px;
  line-height: 1.55;
}
.code-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.code-cards { display: none; }
.code-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.code-card + .code-card { margin-top: 0; }
.code-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.code-card .code-cell { font-size: 22px; letter-spacing: .08em; font-weight: 700; }
.code-card-meta { color: var(--muted); font-size: 13px; margin-bottom: 12px; }
.code-card-actions { display: flex; flex-direction: column; gap: 8px; }
.code-card-actions .btn { width: 100%; min-height: 44px; }
.code-card .sel-row { display: flex; align-items: center; gap: 8px; min-height: 44px; font-weight: 600; }
.code-card .sel-row input { width: 20px; height: 20px; }

@media (max-width: 800px) {
  body.portal-member { overflow-x: hidden; }
  body.portal-member .login-card { padding: 28px 20px; }
  body.portal-member .login-card h1 { font-size: 24px; }
  body.portal-member .login-card .hint { font-size: 15px; }
  body.portal-member input[type=text],
  body.portal-member input[type=password],
  body.portal-member input[type=number],
  body.portal-member input[type=url],
  body.portal-member select,
  body.portal-member textarea {
    min-height: 44px;
    font-size: 16px;
    padding: 11px 12px;
  }
  body.portal-member .btn,
  body.portal-member .menu-btn {
    min-height: 44px;
    padding: 12px 16px;
    font-size: 16px;
    border-radius: 12px;
  }
  body.portal-member .btn.sm {
    min-height: 44px;
    padding: 12px 16px;
    font-size: 16px;
  }
  body.portal-member .topbar { height: auto; min-height: var(--top); padding-top: 8px; padding-bottom: 8px; }
  body.portal-member .menu-btn { min-width: 44px; }
  body.portal-member .filters { flex-direction: column; align-items: stretch; }
  body.portal-member .filters .field { min-width: 0; width: 100%; }
  body.portal-member .filters .actions { width: 100%; flex-direction: column; }
  body.portal-member .filters .actions .btn { width: 100%; }
  body.portal-member .section-head { flex-direction: column; align-items: stretch; }
  body.portal-member .section-head .btn { width: 100%; }
  body.portal-member .panel { overflow: visible; }
  body.portal-member .desktop-table { display: none; }
  body.portal-member .code-cards { display: flex; flex-direction: column; gap: 12px; padding: 12px; }
  body.portal-member .main {
    /* 先给一个不依赖 env()/calc 的足够留白，避免部分 WebView 整条声明失效后回退到过小值 */
    padding-bottom: 160px;
    padding-bottom: calc(var(--bottom-nav-h) + var(--main-bottom-extra) + env(safe-area-inset-bottom, 0px));
  }
  body.portal-member .bottom-nav {
    min-height: var(--bottom-nav-h);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  body.portal-member .bottom-nav a {
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    padding: 10px 6px;
  }
  body.portal-member .pager { justify-content: stretch; flex-wrap: wrap; }
  body.portal-member .pager .btn { flex: 1; min-height: 44px; }
  body.portal-member .code-result { font-size: 20px; letter-spacing: .08em; }
  body.portal-member .gen-actions { display: flex; flex-direction: column; gap: 8px; }
  body.portal-member .gen-actions .btn { width: 100%; }
  body.portal-member .stats-cards { display: flex; flex-direction: column; gap: 8px; }
  body.portal-member .stats-cards .stat-row { display: flex; justify-content: space-between; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  body.portal-member .toast-wrap { top: auto; bottom: calc(64px + env(safe-area-inset-bottom, 0px)); right: 12px; left: 12px; }
  body.portal-member .toast { min-width: 0; width: 100%; max-width: none; }
}

/* Desktop keeps tables; cards stay hidden above 800px */
@media (min-width: 801px) {
  .code-cards { display: none !important; }
}


/* Contact QR, renew, selling products */
.qr-box { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 8px 0 4px; }
.qr-box img.qr-img {
  width: min(280px, 80vw);
  height: auto;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  padding: 8px;
  image-rendering: auto;
}
.qr-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; width: 100%; }
.qr-lightbox {
  position: fixed; inset: 0; background: rgba(15,23,42,.72); z-index: 80;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.qr-lightbox img {
  width: min(440px, 92vw); height: auto; max-height: 86vh; background: #fff;
  padding: 12px; border-radius: 12px;
}
.selling-card { display: flex; flex-direction: column; gap: 10px; }
.selling-card .selling-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.selling-card .btn { min-height: 44px; }
.home-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 16px; }
.contact-preview { max-width: 240px; width: 100%; height: auto; border: 1px solid var(--line); border-radius: 12px; background: #fff; padding: 8px; }
.file-btn { position: relative; overflow: hidden; display: inline-flex; align-items: center; justify-content: center; }
.file-btn input[type=file] { position: absolute; inset: 0; opacity: 0; cursor: pointer; font-size: 0; }

@media (max-width: 800px) {
  body.portal-member .home-actions { flex-direction: column; }
  body.portal-member .home-actions .btn { width: 100%; }
  body.portal-member .selling-card .btn { width: 100%; }
  body.portal-member .qr-box img.qr-img { width: min(320px, 86vw); }
  body.portal-member .qr-actions .btn { width: 100%; }
  body.portal-member .bottom-nav a { font-size: 13px; padding: 10px 4px; }
  body.portal-admin .filters { flex-wrap: wrap; }
}


/* Phase 3: log/bill cards, admin mobile, no page-wide horizontal scroll */
.log-cards, .stack-cards { display: none; }
.log-card, .stack-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.log-card + .log-card, .stack-card + .stack-card { margin-top: 0; }
.log-card-head, .stack-card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px;
}
.log-card-meta, .stack-card-meta { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.log-card-detail { font-size: 14px; word-break: break-word; }
.risk-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; padding: 0 5px; margin-left: 6px;
  border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700;
}
.nav a .risk-badge { background: #ffb4b4; color: #7f1d1d; }
.amount { font-variant-numeric: tabular-nums; font-weight: 650; }
.amount.neg { color: var(--danger); }
.amount.pos { color: var(--success); }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 800px) {
  html, body { overflow-x: hidden; max-width: 100%; }
  body.portal-member, body.portal-admin { overflow-x: hidden; }
  body.portal-admin { padding-bottom: 0; }
  body.portal-admin .main {
    padding-bottom: 48px;
    padding-bottom: calc(48px + env(safe-area-inset-bottom, 0px));
  }
  body.portal-admin.admin-has-bottom .main {
    padding-bottom: 160px;
    padding-bottom: calc(var(--bottom-nav-h) + var(--main-bottom-extra) + env(safe-area-inset-bottom, 0px));
  }
  body.portal-admin .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    background: #fff; border-top: 1px solid var(--line);
    min-height: var(--bottom-nav-h);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  body.portal-admin .bottom-nav a {
    flex: 1; text-align: center; color: var(--muted);
    text-decoration: none; font-size: 13px; font-weight: 650;
    min-height: 48px; display: flex; align-items: center; justify-content: center;
    padding: 10px 4px;
  }
  body.portal-admin .bottom-nav a.active { color: var(--accent); }
  body.portal-admin .btn,
  body.portal-admin .menu-btn,
  body.portal-member .btn,
  body.portal-member .menu-btn {
    min-height: 44px;
  }
  body.portal-admin input[type=text],
  body.portal-admin input[type=password],
  body.portal-admin input[type=number],
  body.portal-admin input[type=url],
  body.portal-admin input[type=date],
  body.portal-admin select,
  body.portal-admin textarea,
  body.portal-member input[type=date] {
    min-height: 44px;
    font-size: 16px;
  }
  body.portal-admin .filters { flex-direction: column; align-items: stretch; }
  body.portal-admin .filters .field { min-width: 0; width: 100%; }
  body.portal-admin .filters .actions { width: 100%; flex-direction: column; }
  body.portal-admin .filters .actions .btn,
  body.portal-admin .section-head .btn { width: 100%; }
  body.portal-admin .section-head { flex-direction: column; align-items: stretch; }
  body.portal-admin .panel { overflow: visible; }
  body.portal-admin .desktop-table,
  body.portal-member .desktop-table { display: none; }
  body.portal-admin .code-cards,
  body.portal-admin .log-cards,
  body.portal-admin .stack-cards,
  body.portal-member .log-cards,
  body.portal-member .stack-cards {
    display: flex; flex-direction: column; gap: 12px; padding: 12px;
  }
  body.portal-admin .nav a, body.portal-admin .nav button.nav-link,
  body.portal-member .nav a {
    min-height: 44px;
  }
}

@media (min-width: 801px) {
  .log-cards, .stack-cards { display: none !important; }
}
