:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #ebe9e3;
  --text: #1d2321;
  --muted: #66706c;
  --line: #dcd9d1;
  --accent: #1f6f5c;
  --accent-ink: #ffffff;
  --accent-soft: #dcefe8;
  --warn: #9a5b00;
  --warn-soft: #fbecd2;
  --danger: #b3261e;
  --shadow: 0 6px 24px rgb(20 30 25 / 0.12);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111715;
    --surface: #1a2220;
    --surface-2: #232d2a;
    --text: #e7ece9;
    --muted: #9aa6a1;
    --line: #2e3a36;
    --accent: #5cc3a5;
    --accent-ink: #0b1a15;
    --accent-soft: #1d3a32;
    --warn: #f0b45c;
    --warn-soft: #3a2c15;
    --danger: #ff8a80;
    --shadow: 0 6px 24px rgb(0 0 0 / 0.4);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111715;
  --surface: #1a2220;
  --surface-2: #232d2a;
  --text: #e7ece9;
  --muted: #9aa6a1;
  --line: #2e3a36;
  --accent: #5cc3a5;
  --accent-ink: #0b1a15;
  --accent-soft: #1d3a32;
  --warn: #f0b45c;
  --warn-soft: #3a2c15;
  --danger: #ff8a80;
  --shadow: 0 6px 24px rgb(0 0 0 / 0.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ヘッダー */
.top {
  position: sticky;
  top: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}

.top-row { display: flex; align-items: center; justify-content: space-between; }

h1 { font-size: 20px; margin: 0; letter-spacing: 0.02em; }

.summary { display: flex; gap: 16px; margin: 4px 0 10px; color: var(--muted); font-size: 13px; }
.summary b { color: var(--text); font-size: 22px; font-variant-numeric: tabular-nums; margin-right: 2px; }
.summary .warn { color: var(--warn); }

.search { display: flex; gap: 8px; }
.search input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 9px 12px;
}

.filters {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-top: 10px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--muted);
}
.filters label { display: grid; gap: 4px; }
.range { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.range input { flex: 1; min-width: 0; }
.filters input, .filters select, .fields input, .fields select, .fields textarea, .range-row input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 9px 10px;
  color: var(--text);
  min-height: 44px;
}

/* ボタン類 */
.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; background: transparent; border-radius: 12px;
  color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }

.chip-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 14px;
  white-space: nowrap;
}
.chip-btn[aria-expanded="true"], .chip-btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.text-btn { border: 0; background: none; color: var(--accent); padding: 8px 4px; font-size: 15px; justify-self: start; }
.text-btn.danger { color: var(--danger); }

.primary-btn, .secondary-btn {
  display: flex; align-items: center; justify-content: center;
  width: 100%;
  min-height: 48px;
  border-radius: 12px;
  font-weight: 600;
  border: 0;
  cursor: pointer;
}
.primary-btn { background: var(--accent); color: var(--accent-ink); }
.primary-btn.small { width: auto; min-height: 38px; padding: 0 18px; }
.primary-btn:disabled { opacity: 0.5; }
.secondary-btn { background: var(--surface-2); color: var(--text); margin-top: 8px; }

/* 一覧 */
.list { padding: 8px 16px 24px; max-width: 720px; margin: 0 auto; }

.month { margin-top: 18px; }
.month-head {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 13px; color: var(--muted); padding: 0 4px 6px;
}
.month-head b { color: var(--text); font-variant-numeric: tabular-nums; font-size: 15px; }

.items { background: var(--surface); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); }

.item {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  gap: 12px;
  align-items: center;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-top: 1px solid var(--line);
  background: none;
  text-align: left;
  cursor: pointer;
}
.item:first-child { border-top: 0; }
.item:active { background: var(--surface-2); }
.item img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; background: var(--surface-2); }
.item .meta { min-width: 0; }
.item .vendor { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.item .amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.item.deleted { opacity: 0.55; }
.item.deleted .vendor { text-decoration: line-through; }

.badge { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.warn { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.badge.busy { background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 64px 24px; }
.empty svg { width: 56px; height: 56px; stroke-width: 1.2; margin-bottom: 8px; }

/* 撮影ボタン */
.capture-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex; gap: 10px; justify-content: center;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 60%, transparent);
  z-index: 4;
}
.camera-btn, .album-btn {
  display: flex; align-items: center; gap: 8px;
  border-radius: 999px;
  font-weight: 700;
  box-shadow: var(--shadow);
  cursor: pointer;
  user-select: none;
}
.camera-btn { background: var(--accent); color: var(--accent-ink); padding: 15px 26px; font-size: 17px; }
.album-btn { background: var(--surface); color: var(--text); padding: 15px 18px; font-size: 15px; border: 1px solid var(--line); }
.camera-btn:active, .album-btn:active { transform: scale(0.97); }

/* シート（編集・設定） */
dialog.sheet {
  width: 100%;
  max-width: 640px;
  height: 100%;
  max-height: 100%;
  margin: 0 auto;
  padding: 0;
  border: 0;
  background: var(--bg);
  color: var(--text);
}
dialog.sheet::backdrop { background: rgb(0 0 0 / 0.45); }
@media (min-width: 700px) {
  dialog.sheet { height: auto; max-height: 92vh; margin: 4vh auto; border-radius: 18px; }
}

.sheet-inner { padding: 0 16px calc(32px + env(safe-area-inset-bottom)); }

.sheet-head {
  position: sticky; top: 0; z-index: 2;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  margin: 0 -16px 12px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.sheet-head > :last-child { justify-self: end; }

.photo { margin: 0; }
.photo img {
  display: block;
  width: 100%;
  max-height: 46vh;
  object-fit: contain;
  background: var(--surface-2);
  border-radius: var(--radius);
  cursor: zoom-in;
}
.photo.zoom img { max-height: none; cursor: zoom-out; }
.photo figcaption { font-size: 12px; color: var(--muted); margin-top: 4px; }
.photo figcaption .warn { color: var(--warn); }

.ocr-status { margin: 12px 0 0; padding: 10px 12px; background: var(--accent-soft); color: var(--accent); border-radius: 12px; font-size: 14px; display: grid; gap: 6px; }
.ocr-status progress { width: 100%; height: 6px; accent-color: var(--accent); }

.fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}
.fields label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); min-width: 0; }
.fields .wide { grid-column: 1 / -1; }
.fields input:focus, .fields select:focus, .fields textarea:focus, .search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.fields .filled { background: var(--accent-soft); }
.yen { position: relative; display: block; }
.yen::before { content: "¥"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.yen input { padding-left: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }

.more { margin-top: 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 4px 12px; }
.more summary { padding: 8px 0; cursor: pointer; font-size: 14px; }
.more pre { white-space: pre-wrap; word-break: break-all; font-size: 12px; color: var(--muted); margin: 0 0 8px; max-height: 240px; overflow: auto; }

.history { margin: 0 0 8px; padding-left: 20px; font-size: 12px; color: var(--muted); }
.history li { margin-bottom: 6px; }
.history time { color: var(--text); }

.danger-zone { margin-top: 20px; text-align: center; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px 16px; margin-bottom: 14px; }
.panel h2 { font-size: 16px; margin: 0 0 4px; }
.note { font-size: 13px; color: var(--muted); margin: 0 0 12px; }
.list-note { padding-left: 18px; margin: 0; }
.list-note li { margin-bottom: 6px; }
.range-row { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.range-row input { flex: 1; min-width: 0; }
.preset-row { display: flex; gap: 6px; flex-wrap: wrap; margin: 10px 0 12px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  box-shadow: var(--shadow);
  z-index: 100;
}

@media (min-width: 560px) {
  .filters { grid-template-columns: 1fr 1fr; }
}
