:root {
  --paper: #f8f9fa;
  --card: #ffffff;
  --ink: #1a1a2e;
  --muted: #6b7280;
  --faint: #9ca3af;
  --line: #e5e7eb;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --soft: #f3f4f6;
  --warn: #92400e;
  --warn-bg: rgba(245, 158, 11, 0.12);
  --danger: #f43f5e;
  --top-h: 60px;
  --dock-h: 0px;
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100vh; min-height: 100dvh; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:active:not(:disabled):not(.brand) { transform: scale(0.96); }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.25rem; font-weight: 700; }
h2 { font-size: 0.95rem; font-weight: 700; }
.group { margin-top: 8px; font-size: 0.75rem; font-weight: 700; color: var(--muted); }

.top {
  display: flex;
  gap: 16px;
  align-items: center;
  padding: max(14px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) 14px max(24px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.92);
  position: sticky;
  top: 0;
  z-index: 4;
}
.brand { display: flex; flex-direction: column; gap: 2px; background: none; border: 0; padding: 0; text-align: left; margin-right: auto; min-width: 0; }
.brand strong { font-size: 1rem; font-weight: 700; }
.brand span, .status { color: var(--muted); font-size: 0.75rem; font-weight: 500; }
.status { text-align: right; }
.status:empty { display: none; }

.layout { display: grid; grid-template-columns: 220px minmax(0, 1fr); align-items: start; min-height: calc(100vh - var(--top-h)); min-height: calc(100dvh - var(--top-h)); }
.dock {
  position: sticky;
  top: var(--top-h);
  align-self: start;
  max-height: calc(100vh - var(--top-h));
  max-height: calc(100dvh - var(--top-h));
  overflow: auto;
  z-index: 3;
  border-right: 1px solid var(--line);
  background: var(--paper);
}
.dock-more { display: none; }
.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px 14px 28px;
}
.nav button {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 12px;
  padding: 10px 12px;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 500;
}
.nav button.active, .nav button:hover { background: var(--card); color: var(--ink); font-weight: 700; }
.nav button.active { box-shadow: 0 1px 2px rgba(26, 26, 46, 0.06); }
.main {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 16px;
  padding: 28px max(32px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(32px, env(safe-area-inset-left));
  max-width: 1080px;
  min-width: 0;
}
.main > * { min-width: 0; max-width: 100%; }
.main > .primary, .main > .ghost, .main > .danger { align-self: flex-start; }

.banner {
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid rgba(245, 158, 11, 0.35);
  border-radius: 12px;
  padding: 14px 16px;
  line-height: 1.55;
}
.list { display: grid; gap: 12px; }
.list:has(> .counter) { gap: 16px; }
.tool, .row {
  padding: 14px 16px;
  text-align: left;
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.row:hover, .tool:hover { border-color: rgba(59, 130, 246, 0.6); box-shadow: 0 4px 8px rgba(26, 26, 46, 0.06); }
.row strong { font-weight: 700; font-size: 0.875rem; }
.tool small, .row small, .muted { color: var(--muted); display: block; margin-top: 2px; font-weight: 500; font-size: 0.75rem; }
.tabs, .inline { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.tabs { margin: 0; }
.tabs button, .primary, .ghost, .danger {
  border-radius: 999px;
  border: 1px solid rgba(229, 231, 235, 0.9);
  background: white;
  padding: 8px 16px;
  font-weight: 700;
  font-size: 0.75rem;
}
.tabs button { color: var(--muted); min-height: 32px; }
.tabs button.active { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.primary { background: var(--accent); color: white; border-color: var(--accent); min-height: 36px; }
.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.ghost { color: var(--ink); }
.danger { color: var(--danger); background: white; }
.search { margin: 0; border-radius: 999px; }
.search, input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: white;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search:focus, input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}
.search { border-radius: 999px; }
input[type="checkbox"] { width: 1.15rem; height: 1.15rem; padding: 0; accent-color: var(--accent); }
label.check { display: flex; align-items: center; gap: 10px; min-height: 44px; }
input[type="file"] { font-size: 0.92rem; }
textarea { min-height: 78px; resize: vertical; }
label { display: grid; gap: 8px; font-size: 0.92rem; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 160px), 1fr)); gap: 16px; margin: 0; }
.stack { display: grid; gap: 16px; }
.empty { color: var(--muted); margin: 0; padding: 16px; }
.row { display: flex; justify-content: space-between; gap: 16px; align-items: center; }
.row b { flex: none; white-space: nowrap; }
.thumb { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; background: var(--paper); border: 1px solid var(--line); }
.photo { width: min(100%, 480px); max-height: 360px; object-fit: contain; border-radius: 12px; border: 1px solid var(--line); background: var(--card); display: block; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.result { padding: 16px 18px; background: rgba(59, 130, 246, 0.08); border-radius: 12px; line-height: 1.55; }
.result .note { margin-top: 8px; }
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; background: white; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: var(--card); }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--line); font-size: 0.92rem; white-space: nowrap; }
td:last-child { white-space: normal; }
tr.hit { background: rgba(59, 130, 246, 0.1); }
.counter {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 20px;
  align-items: center;
  padding: 18px 20px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(26, 26, 46, 0.04);
}
.counter-meta { display: grid; gap: 12px; min-width: 0; }
.count-pad { display: grid; justify-items: end; gap: 12px; }
.count { font-variant-numeric: tabular-nums; font-size: 2.4rem; line-height: 1; }
.chart-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 12px; background: white; padding: 8px; overscroll-behavior: contain; }
canvas { display: block; max-width: 100%; }
#chart { touch-action: none; height: auto; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.editor { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.editor-settings { display: grid; gap: 16px; min-width: 0; }
.editor-chart, .editor-side, .editor-body { min-width: 0; }
.chart-pan[hidden] { display: none; }
#preview { width: 100%; height: auto; background: white; border: 1px solid var(--line); border-radius: 12px; }
.palette { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin: 0; padding: 4px 2px; }
.swatch-color { width: 28px; height: 28px; border-radius: 50%; border: 2px solid white; box-shadow: 0 0 0 1px var(--line); padding: 0; }
.swatch-color.active { box-shadow: 0 0 0 2px var(--accent); }
.error { color: var(--danger); }
.note { color: var(--muted); font-size: 0.9rem; line-height: 1.5; margin: 0; }
.split { display: grid; grid-template-columns: minmax(0, 280px) minmax(0, 1fr); gap: 24px; align-items: start; }
.split > div { display: grid; gap: 12px; align-content: start; min-width: 0; }

@media (max-width: 800px) {
  .top {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "brand status";
    gap: 8px 12px;
    align-items: center;
    padding: max(12px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 12px max(20px, env(safe-area-inset-left));
    background: rgba(255, 255, 255, 0.92);
  }
  .brand { grid-area: brand; }
  .brand span { display: none; }
  .brand strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .layout, .split { display: block; }
  .dock {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: none;
    overflow: visible;
    border-right: 0;
    border-top: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.96);
    z-index: 6;
    padding-bottom: env(safe-area-inset-bottom);
    box-shadow: 0 -8px 24px rgba(26, 26, 46, 0.05);
  }
  .dock::before, .dock::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: env(safe-area-inset-bottom);
    width: 28px;
    pointer-events: none;
    opacity: 0;
    z-index: 1;
  }
  .dock.can-left::before { opacity: 1; left: 0; background: linear-gradient(to right, #fff, rgba(255, 255, 255, 0)); }
  .dock.can-right::after { opacity: 1; right: 0; background: linear-gradient(to left, #fff, rgba(255, 255, 255, 0)); }
  .dock.can-right .nav {
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - var(--dock-fade, 28px)), transparent calc(100% - var(--dock-fade, 28px) + 12px));
    mask-image: linear-gradient(to right, #000 calc(100% - var(--dock-fade, 28px)), transparent calc(100% - var(--dock-fade, 28px) + 12px));
  }
  .dock-more {
    display: none;
    position: absolute;
    z-index: 2;
    top: 21px;
    right: max(6px, env(safe-area-inset-right));
    width: 22px;
    height: 22px;
    align-items: center;
    justify-content: center;
    color: var(--ink);
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 0 0 3px #fff;
    pointer-events: none;
  }
  .dock.can-right .dock-more { display: flex; }
  .dock-more svg { width: 14px; height: 14px; animation: dock-more-slide 1.15s ease-in-out infinite; }
  @keyframes dock-more-slide {
    0%, 100% { transform: translateX(-3px); opacity: 0.72; }
    50% { transform: translateX(4px); opacity: 1; }
  }
  .nav {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
    scroll-padding-inline: 16px;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
    background: transparent;
    overscroll-behavior-x: contain;
  }
  .nav::-webkit-scrollbar { display: none; }
  .nav button {
    width: auto;
    flex: none;
    white-space: nowrap;
    min-height: 44px;
    height: 44px;
    padding: 0 14px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: white;
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 700;
    box-shadow: none;
    scroll-snap-align: center;
  }
  .nav button.active { background: var(--ink); color: var(--paper); border-color: var(--ink); }
  .main {
    gap: 18px;
    padding: 20px max(20px, env(safe-area-inset-right)) calc(var(--dock-h) + 24px) max(20px, env(safe-area-inset-left));
  }
  .main > .primary { align-self: stretch; }
  h1 { font-size: 1.15rem; }
  .inline:has(> h1) { justify-content: space-between; }
  .tabs button, .primary, .danger, .actions .ghost, .palette .ghost { min-height: 44px; }
  input, select, textarea { scroll-margin-top: 12px; scroll-margin-bottom: calc(var(--dock-h) + 76px); }
  input[type="file"] { min-height: 44px; }
  .spend { flex-direction: column; align-items: stretch; }
  .trio { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .trio input { padding-left: 8px; padding-right: 8px; }
  .actions.bar {
    position: sticky;
    bottom: var(--dock-h);
    z-index: 4;
    margin-left: calc(-1 * max(20px, env(safe-area-inset-left)));
    margin-right: calc(-1 * max(20px, env(safe-area-inset-right)));
    padding: 12px max(20px, env(safe-area-inset-right)) 12px max(20px, env(safe-area-inset-left));
    background: rgba(248, 249, 250, 0.96);
    border-top: 1px solid var(--line);
  }
  .actions.bar .primary { flex: 1; }
  .counter { grid-template-columns: 1fr; }
  .count-pad, .counter .actions { width: 100%; }
  .count-pad { justify-items: stretch; }
  .count { font-size: 3.4rem; text-align: center; }
  .stepper { flex-wrap: nowrap; }
  .stepper span { flex: 1; text-align: center; }
  .stepper .ghost { min-width: 44px; min-height: 44px; padding: 0 12px; }
  .counter .actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "sub reset" "add add" "drop drop";
    gap: 12px;
  }
  .counter [data-act="count"][data-by="-1"] { grid-area: sub; }
  .counter [data-act="count"][data-by="1"] { grid-area: add; min-height: 72px; font-size: 1.45rem; }
  .counter [data-act="reset"] { grid-area: reset; }
  .counter [data-act="drop-counter"] { grid-area: drop; min-height: 36px; border: 0; background: transparent; color: var(--muted); font-weight: 500; }
  .swatch-color { width: 44px; height: 44px; }
  .row { min-height: 64px; }
  .row span { min-width: 0; }
  .row strong, .row small { overflow-wrap: anywhere; }
  .thumb { width: 56px; height: 56px; flex: none; }
  .editor { gap: 18px; }
  .editor-settings { order: 0; }
  .palette { order: 1; }
  .editor-body { order: 2; }
  #chart { width: auto; max-width: none; height: auto; }
  .split > * + * { margin-top: 20px; }
  .table-scroll th, .table-scroll td { white-space: nowrap; font-size: 0.85rem; padding: 12px 14px; }
  .table-scroll th:first-child, .table-scroll td:first-child {
    position: sticky;
    left: 0;
    background: #fff;
    z-index: 1;
    box-shadow: 8px 0 8px rgba(26, 26, 46, 0.03);
  }
  .table-scroll tr.hit td:first-child { background: #e8f1fe; }
}

@media (prefers-reduced-motion: reduce) {
  .dock-more svg { animation: none; opacity: 0.85; }
}
