:root {
  --bg: #efece6;
  --paper: #fbf9f6;
  --ink: #1e1b16;
  --muted: #726c64;
  --line: #e3ddd3;
  --accent: #0e6b4a;
  --accent-press: #0b553b;
  --danger: #8e332c;
  --warn-bg: #f8f1e4;
  --warn-ink: #7a5420;
  --shadow: 0 1px 2px rgba(30, 27, 22, 0.05);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Sans", "Yu Gothic", "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

button, input, textarea {
  font: inherit;
  color: inherit;
}

button { cursor: pointer; }

.app {
  width: min(480px, 100%);
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
}

body[data-screen="camera"] .app { padding: 0; width: 100%; }
body[data-screen="camera"] .top,
body[data-screen="camera"] .banner,
body:not([data-screen="ledger"]) .action-bar { display: none; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.header-rect {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: max-content;
  min-height: 40px;
  padding: 8px 14px;
  border: 0;
  border-radius: 0;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
}

.header-rect[hidden] { display: none; }

#logout {
  background: #fff;
  color: #d1242f;
  border: 1px solid #d1242f;
}

#ledger-clear {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
}

body[data-screen="login"] .top-actions,
body[data-screen="register"] .top-actions,
body[data-screen="forgot"] .top-actions,
body[data-screen="reset"] .top-actions { display: none; }

.password-field { position: relative; display: block; }
.password-field input { padding-right: 44px; }
.password-toggle {
  position: absolute;
  top: 50%;
  right: 6px;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  transform: translateY(-50%);
}
.password-toggle .eye-off { display: none; }
.password-toggle.is-visible .eye { display: none; }
.password-toggle.is-visible .eye-off { display: block; }

.auth-links {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
}
.auth-link {
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
  text-decoration: underline;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 4px;
  flex: 1 1 auto;
}

.master-dialog {
  width: min(1280px, calc(100vw - 16px));
  max-height: calc(100vh - 16px);
  margin: auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--bg);
  color: inherit;
  box-shadow: var(--shadow);
  overflow: auto;
}

.master-dialog::backdrop {
  background: rgb(28 24 20 / 45%);
}

.master-dialog-body {
  display: grid;
  gap: 12px;
  padding: 20px;
}

.master-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.master-dialog-head h2 { font-size: 20px; }

.master-dialog-head button {
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  font-size: 13px;
  font-weight: 700;
}

.master-dialog .lead { margin: 0; }

.master-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin: 0;
}

@media (min-width: 1100px) {
  .master-grid { grid-template-columns: 1fr 1fr; }
}

.master-card {
  display: grid;
  align-content: start;
  gap: 12px;
  padding: 16px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 0;
}

.master-add {
  display: grid;
  align-items: start;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.master-add input { min-width: 0; }
.master-add .primary {
  grid-column: 1 / -1;
  border-radius: 0;
}

.master-dialog input,
.master-filter {
  border-radius: 0;
}

.master-filter {
  min-height: 40px;
  padding: 8px 12px;
}

.master-sheet {
  border: 1px solid #b4b4b4;
  background: #fff;
}

.master-sheet-head,
.master-list li {
  display: grid;
  grid-template-columns: 36px minmax(0, 1.35fr) minmax(0, 1.15fr) 44px;
  align-items: stretch;
  height: 30px;
  min-height: 30px;
  border-bottom: 1px solid #b4b4b4;
  background: #fff;
}

.master-sheet-head {
  background: #f3f3f3;
  color: #333;
  font-size: 13px;
  font-weight: 700;
}

.master-sheet-head span,
.master-no,
.master-list button {
  display: grid;
  place-items: center;
  min-width: 0;
  border-left: 1px solid #b4b4b4;
}

.master-sheet-head span:first-child,
.master-no { border-left: 0; }

.master-list {
  display: block;
  max-height: calc(20 * 30px);
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.master-list li:last-child { border-bottom: 0; }

.master-no {
  color: #666;
  font-size: 12px;
  background: #f7f7f7;
}

.master-item-main {
  display: contents;
}

.master-list input {
  width: 100%;
  min-height: 0;
  height: 100%;
  padding: 0 6px;
  border: 0;
  border-left: 1px solid #b4b4b4;
  border-radius: 0;
  background: transparent;
  font-size: 14px;
  font-weight: 400;
}

.master-list input:focus {
  outline: 2px solid #3d8b40;
  outline-offset: -2px;
  background: #e5f2e5;
}

.master-list button {
  padding: 0;
  border-top: 0;
  border-right: 0;
  border-bottom: 0;
  border-radius: 0;
  background: transparent;
  color: #b42318;
  font-size: 12px;
  font-weight: 700;
}

.master-list li.master-empty {
  display: block;
  height: auto;
  min-height: 0;
  padding: 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.dl-menu { position: relative; }

.dl-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  display: grid;
  gap: 4px;
  min-width: max-content;
  padding: 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.dl-panel[hidden] { display: none; }

.dl-panel button {
  min-height: 40px;
  padding: 8px 12px;
  border: 0;
  border-radius: 8px;
  background: #f4f0e8;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  text-align: left;
  white-space: nowrap;
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

h1, h2 { margin: 0; font-weight: 700; }
h1 { font-size: 28px; line-height: 1.2; }
h2 { font-size: 22px; }

.lead, .summary { color: var(--muted); }
.summary { margin: 0 0 12px; }

.banner, .toast, .form-error, .camera-error {
  border-radius: 12px;
  padding: 10px 12px;
}

.banner {
  margin: 0 0 12px;
  background: var(--warn-bg);
  color: var(--warn-ink);
}

.toast {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  left: 50%;
  z-index: 40;
  width: min(440px, calc(100% - 32px));
  margin: 0;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  text-align: center;
}

.form-error, .camera-error {
  margin: 0;
  background: #f8e8e6;
  color: var(--danger);
}

.panel {
  display: grid;
  gap: 14px;
  padding: 16px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

label {
  display: grid;
  gap: 6px;
  font-size: 14px;
  font-weight: 700;
}

input, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  font-weight: 400;
}

textarea { resize: vertical; }

.primary, .secondary, .danger, .ghost, .text-button, .file-fallback {
  min-height: 48px;
  border-radius: 14px;
  border: 0;
}

.primary, .secondary, .danger { padding: 12px 16px; font-weight: 700; }
label.primary, label.secondary, label.shoot {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.primary { background: var(--accent); color: #fff; }
.primary:active { background: var(--accent-press); }
.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.danger { background: #fff; color: var(--danger); border: 1px solid #efd0cc; }
.text-button, .ghost, .file-fallback { background: transparent; }
.text-button { min-height: 40px; color: var(--muted); padding: 8px; }
.back {
  justify-self: start;
  min-height: 40px;
  margin: 0 0 8px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}
.ghost { color: #fff; padding: 8px 4px; }

.primary:disabled { opacity: 0.6; cursor: wait; }

.row { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; }

body[data-screen="review"] .app {
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

body[data-screen="review"] .row {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 15;
  width: min(480px, 100%);
  margin: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: linear-gradient(transparent, var(--bg) 28%);
}

.list { display: grid; gap: 10px; }

.card {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  width: 100%;
  padding: 12px;
  text-align: left;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.card img, #preview, #detail-image {
  width: 100%;
  object-fit: cover;
  background: #ece7df;
}

.card img {
  height: 72px;
  border-radius: 10px;
}

#preview, #detail-image {
  object-fit: contain;
  border-radius: 12px;
}

#preview { max-height: 220px; }
#detail-image { max-height: 42vh; }

.totals {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 0 0 12px;
}

.totals div {
  padding: 10px 8px;
  text-align: center;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.totals span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.totals strong { font-size: 15px; }

.money-form { display: grid; gap: 14px; }

.card-title { font-weight: 700; }
.card-sub, .card-amount { color: var(--muted); font-size: 14px; }
.card-amount { color: var(--ink); }

.pill {
  display: inline-block;
  margin-top: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #f4ead7;
  color: #8a5a12;
  font-size: 12px;
  font-weight: 700;
}

.empty {
  padding: 36px 12px;
  text-align: center;
  color: var(--muted);
}

.empty svg {
  width: 64px;
  height: 64px;
  color: #b7b0a6;
}

.empty p { margin: 8px 0; }

.action-bar {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 10;
  display: grid;
  gap: 4px;
  width: min(480px, 100%);
  transform: translateX(-50%);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 28%);
}

.action-bar .primary { width: 100%; }

.sub-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.sub-actions .shoot {
  width: max-content;
}

.shoot { position: relative; overflow: hidden; }

.offscreen-file {
  position: fixed;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.shoot input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: transparent;
  opacity: 0;
  font-size: 16px;
  cursor: pointer;
}

[data-view="camera"] {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: #111;
}

#camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #111;
}

.camera-shade {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 180px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.55));
  pointer-events: none;
}

.camera-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(64px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 20px;
}

.camera-bar .ghost:last-child { justify-self: end; }

.shutter {
  width: 74px;
  height: 74px;
  border: 4px solid #fff;
  border-radius: 50%;
  background: transparent;
  box-shadow: inset 0 0 0 6px rgba(255, 255, 255, 0.25);
}

.shutter:active { background: rgba(255, 255, 255, 0.35); }

.file-actions {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(8px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  gap: 18px;
}

.file-actions .file-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.camera-error {
  position: absolute;
  top: calc(16px + env(safe-area-inset-top));
  left: 16px;
  right: 16px;
}

body[data-screen="ledger"] .app {
  width: min(1100px, 100%);
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

.replace-bar {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.replace-bar { grid-template-columns: 1fr 1fr auto; }

@media (max-width: 720px) {
  .replace-bar { grid-template-columns: 1fr; }
}

.ledger-scroll {
  max-height: min(70vh, 640px);
  overflow-x: hidden;
  overflow-y: auto;
  border: 1px solid #b4b4b4;
  border-radius: 16px;
  background: #fff;
}

#ledger-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 13px;
  background: #fff;
}

#ledger-table col.col-num { width: 5%; }
#ledger-table col.col-date { width: 8%; }
#ledger-table col.col-money { width: 8%; }
#ledger-table col.col-text { width: 12.5%; }
#ledger-table col.col-voucher { width: 6%; }
#ledger-table col.col-action { width: 9%; }

#ledger-table th,
#ledger-table td {
  padding: 6px 4px;
  border: 1px solid #b4b4b4;
  background: #fff;
  white-space: normal;
  overflow-wrap: anywhere;
  text-align: right;
  vertical-align: middle;
}

#ledger-table th:nth-child(2),
#ledger-table td:nth-child(2) {
  text-align: center;
}

#ledger-table thead th.num-head,
#ledger-table thead th.action-head,
#ledger-table td.num-cell,
#ledger-table td.action-cell {
  text-align: center;
}

#ledger-table td:nth-child(6),
#ledger-table td:nth-child(7),
#ledger-table td:nth-child(8) {
  text-align: center;
}

#ledger-table thead {
  position: sticky;
  top: 0;
  z-index: 3;
}

#ledger-table thead th {
  background: #f4f0e8;
  font-size: 12px;
}

#ledger-table thead tr.month-caption th {
  background: #fff;
  font-size: 16px;
  font-weight: 700;
  text-align: center;
  padding: 10px 8px;
  border-bottom: 0;
  box-shadow: inset 0 -1px 0 #b4b4b4;
}

#ledger-table thead tr:nth-child(2) th {
  border-top: 0;
  border-bottom: 0;
  box-shadow: inset 0 -1px 0 #b4b4b4;
}

#ledger-table thead tr.filter-row th {
  border-top: 0;
}

.month-switch {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.month-switch button {
  min-height: 36px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font-size: 14px;
  font-weight: 700;
}

.month-switch button:disabled {
  color: #b7b1a8;
  background: #f7f4ee;
}

.month-switch select {
  width: auto;
  min-height: 36px;
  padding: 4px 8px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 700;
}

#ledger-table .col-sort {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  white-space: normal;
}

#ledger-table .col-filter,
#ledger-table td input,
#ledger-table td select {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 28px;
  margin: 0;
  padding: 4px 2px;
  border: 0;
  border-radius: 6px;
  background: #fff;
  font-size: 12px;
  font-weight: 400;
  text-align: inherit;
}

#ledger-table input[type="date"] {
  min-width: 0;
  font-size: 11px;
}

#ledger-table tbody tr.entry td[data-key] { cursor: cell; }
#ledger-table tbody tr.entry.selected td { background: #d7efe3; }

.suggest-list {
  position: fixed;
  z-index: 35;
  display: grid;
  max-height: 220px;
  overflow: auto;
  margin: 0;
  padding: 4px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.suggest-list[hidden] { display: none; }

.suggest-list button {
  min-height: 36px;
  padding: 6px 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  font-weight: 400;
  text-align: left;
}

.suggest-list button.is-active,
.suggest-list button:hover { background: #e7f3ec; }

.suggest-list button.suggest-add {
  margin-top: 4px;
  border-top: 1px solid var(--line);
  border-radius: 0;
  color: var(--accent);
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
}
#ledger-table tr.day-total td { background: #fff7c2; font-weight: 700; }
#ledger-table tr.month-total td { background: #d4eefb; font-weight: 700; }

#ledger-draft input.hint,
#ledger-draft td.draft-hint {
  text-align: center;
  font-weight: 400;
}

#ledger-draft td.draft-hint {
  color: color-mix(in srgb, currentColor 54%, transparent);
}

#ledger-draft input::placeholder {
  color: color-mix(in srgb, currentColor 54%, transparent);
  opacity: 1;
}

#ledger-table tfoot {
  position: sticky;
  bottom: 0;
  z-index: 2;
}

#ledger-table tfoot td {
  background: #fffdf4;
  border-bottom: 0;
  box-shadow: 0 -1px 0 #b4b4b4, inset 0 -1px 0 #b4b4b4;
}

#ledger-table td.num-cell {
  font-variant-numeric: tabular-nums;
}

#ledger-table td.receipt-cell {
  padding: 4px 2px;
}

#ledger-table .row-delete {
  min-height: 0;
  margin: 0;
  padding: 0 2px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--danger);
  font-size: 12px;
  font-weight: 700;
}

.receipt-slot {
  flex: 0 0 auto;
  width: 28px;
  height: 36px;
  min-height: 0;
  padding: 0;
  border: 1px solid #b4b4b4;
  border-radius: 3px;
  background: #fff;
  overflow: hidden;
  cursor: zoom-in;
}

.receipt-thumb {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.receipt-zoom {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 40;
  width: min(360px, calc(100vw - 32px));
  max-height: min(80vh, 640px);
  padding: 6px;
  background: #fff;
  border: 1px solid #b4b4b4;
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(30, 27, 22, 0.2);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.receipt-zoom img {
  display: block;
  width: 100%;
  max-height: min(64vh, 450px);
  object-fit: contain;
}

.meta { display: grid; gap: 10px; margin: 0; }
.meta div { display: grid; gap: 2px; }
.meta dt { color: var(--muted); font-size: 13px; }
.meta dd { margin: 0; font-weight: 700; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
