:root {
  --ink: #1c1915;
  --muted: #5e564c;
  --paper: #f3efe4;
  --card: #fffdf8;
  --line: #e3d9c6;
  --green: #1f4d3a;
  --green-deep: #16382b;
  --cream: #f4f0e6;
  --brown: #7a3418;
  --danger: #9f1239;
  --shadow: 0 10px 30px rgba(48, 32, 12, 0.08);
  --sans: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Yu Gothic", "Noto Sans JP", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 18px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

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

button {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  background: var(--green);
  color: var(--cream);
  padding: calc(14px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left));
}

.topbar-inner,
.wrap {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

#nav-button[hidden] { display: none; }

#admin-screen.active {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.admin-gate {
  width: min(420px, 100%);
  margin: 0 auto;
  padding: 28px 22px 22px;
  text-align: center;
}

.admin-gate h2 {
  margin-top: 18px;
  font-size: 18.72px;
  line-height: 1.4;
}

.admin-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 0;
}

.admin-actions .ghost,
.admin-actions .primary {
  width: 100%;
  min-height: 56px;
  border-radius: 14px;
}

.admin-actions .ghost {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--green-deep);
}

h1, h2 {
  margin: 0;
  font-weight: 700;
  letter-spacing: 0.02em;
}

h1 { font-size: 28px; }

.ghost {
  appearance: none;
  border: 0;
  background: var(--cream);
  color: var(--green-deep);
  border-radius: 999px;
  min-height: 48px;
  padding: 10px 18px;
  font-weight: 700;
}

.wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px max(16px, env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.totals {
  display: block;
}

.total-card {
  display: flex;
  align-items: baseline;
  justify-content: flex-start;
  gap: 28px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 16px 22px;
  box-shadow: var(--shadow);
}

.total-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 0;
}

.total-card .sho {
  font-size: 40px;
}

.total-card span,
.meta,
.field label,
.stat span,
.stat label {
  color: var(--muted);
  font-size: 15px;
}

.total-card strong {
  display: inline;
  margin-top: 0;
  color: var(--brown);
  font-size: 56px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
}

.total-card em {
  display: inline-block;
  margin-left: 4px;
  font-style: normal;
  font-size: 22px;
  letter-spacing: 0;
}

.meta {
  margin: 18px 2px 10px;
}

.rows {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.leftover {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) auto minmax(0, 1.35fr) auto minmax(0, 1.15fr) auto minmax(0, 0.9fr);
  gap: 10px 12px;
  align-items: end;
  min-width: 0;
  margin-top: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 16px;
}

.leftover .stat strong {
  color: var(--brown);
}

.leftover .field label,
.leftover .stat span {
  white-space: nowrap;
}

.op {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 56px;
  font-size: 28px;
  font-weight: 700;
  color: var(--brown);
}

.row.no-qty {
  grid-template-columns: minmax(0, 1.8fr) 110px minmax(0, 120px) auto;
}

.row.no-qty > .field { display: none; }

.row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1.8fr) 110px 110px minmax(0, 120px) auto;
  gap: 10px 12px;
  align-items: end;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 16px;
}

.row:focus-within { z-index: 4; }

.row.complete {
  border-color: #b7cfc2;
  box-shadow: inset 4px 0 0 var(--green);
}

.field { position: relative; min-width: 0; }

.menu-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.kind {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.kind-button {
  appearance: none;
  border: 1px solid #cfc3ad;
  background: #fff;
  color: var(--green-deep);
  border-radius: 14px;
  min-height: 44px;
  padding: 8px 14px;
  font-weight: 700;
  white-space: nowrap;
}

.kind-button[aria-pressed="true"] {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}

.field label,
.stat span,
.stat label {
  display: block;
  margin-bottom: 6px;
}

.stat strong {
  display: block;
  min-height: 56px;
  line-height: 56px;
  font-size: 22px;
  font-variant-numeric: tabular-nums;
}

.stat strong[hidden],
.per-grams[hidden] {
  display: none;
}

input[type="text"],
input[type="password"],
input[type="number"],
select {
  width: 100%;
  min-height: 56px;
  padding: 12px 14px;
  border: 1px solid #cfc3ad;
  border-radius: 14px;
  background: #fff;
  font-size: 20px;
}

select {
  padding-right: 40px;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, #1f4d3a 50%), linear-gradient(135deg, #1f4d3a 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 12px) 50%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}

input:focus,
select:focus {
  outline: 3px solid rgba(31, 77, 58, 0.28);
  border-color: var(--green);
}

.pick.is-empty {
  color: #9a938a;
}

.pick option {
  color: var(--ink);
}

.pick option[value=""] {
  color: #9a938a;
}

.stat { margin: 0; }

.clear-row,
.text-button,
.danger,
.primary {
  appearance: none;
  border-radius: 14px;
  min-height: 56px;
  padding: 12px 16px;
  font-weight: 700;
}

.clear-row,
.text-button {
  border: 0;
  background: transparent;
  color: var(--danger);
}

.primary,
.danger {
  border: 0;
  color: white;
}

.primary { background: var(--green); }
.panel > .primary { margin-top: 12px; }
.danger { background: var(--danger); }

.actions {
  margin-top: 12px;
}

.wide {
  width: 100%;
  border: 1px solid #e4b3c0;
  background: #fff;
}

.screen { display: none; }
.screen.active { display: block; }

#make-screen.screen.active {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px;
  margin-bottom: 14px;
}

.panel h2 { font-size: 20px; margin-bottom: 8px; }

.help { margin: 0 0 14px; color: var(--muted); }

.stack { display: flex; flex-direction: column; gap: 12px; }

.inline {
  display: flex;
  gap: 10px;
  align-items: end;
}

.inline .field { flex: 1; }

.error {
  min-height: 1.4em;
  margin: 8px 0 0;
  color: var(--danger);
  font-weight: 700;
}

.error.ok { color: var(--green); }

.master-list { display: flex; flex-direction: column; gap: 18px; }

.master-group h3 {
  margin: 0 0 10px;
  color: var(--green-deep);
  font-size: 16px;
}

.master-group .empty-group { margin-bottom: 0; }

.master-item {
  display: grid;
  grid-template-columns: 140px minmax(0, 1.4fr) 120px 100px;
  gap: 10px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.master-item.no-qty { grid-template-columns: 140px minmax(0, 1.4fr) 120px; }

.master-item:last-child { border-bottom: 0; padding-bottom: 0; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(28, 25, 21, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 20px;
  z-index: 20;
}

.modal[hidden] { display: none; }

.dialog {
  width: min(520px, 100%);
  background: var(--card);
  border-radius: 22px;
  padding: 22px;
  margin-bottom: env(safe-area-inset-bottom);
}

.dialog h2 { font-size: 24px; }

.dialog p { margin: 10px 0 18px; }

.dialog-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (orientation: portrait), (max-width: 1100px) {
  .topbar-inner {
    align-items: flex-start;
    gap: 10px;
  }

  .topbar-actions {
    flex-direction: column;
    align-items: flex-end;
    gap: 6px;
  }

  h1 { font-size: 24px; }

  .leftover {
    grid-template-columns: 44px minmax(0, 1fr);
    gap: 12px 10px;
  }

  .leftover > .stat:first-child {
    grid-column: 1 / -1;
  }

  .leftover .field label,
  .leftover .stat span {
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "menu menu"
      "per qty"
      "line clear";
  }

  .row > .menu-block { grid-area: menu; }
  .row > .per-stat { grid-area: per; }
  .row > .field { grid-area: qty; }
  .row > .line {
    grid-area: line;
    align-self: end;
  }
  .row.no-qty {
    grid-template-areas:
      "menu menu"
      "per per"
      "line clear";
  }

  .row > .clear-row {
    grid-area: clear;
    align-self: end;
    width: 100%;
    min-height: 56px;
    border: 1px solid #e4b3c0;
    background: #fff;
  }

  .row .stat strong {
    display: flex;
    align-items: center;
    line-height: 1.2;
  }

  .kind-button {
    flex: 1;
    min-height: 48px;
    text-align: center;
  }

  .total-card {
    flex-wrap: wrap;
    gap: 6px 16px;
    padding: 14px 16px;
  }

  .total-card strong { font-size: 40px; }
  .total-card .sho { font-size: 32px; }

  .inline { flex-direction: column; align-items: stretch; }
  .master-item,
  .master-item.no-qty { grid-template-columns: minmax(0, 1fr); }

  .dialog-actions { grid-template-columns: 1fr; }
  .dialog-actions .danger,
  .dialog-actions .text-button { min-height: 52px; }

  .formula { font-size: 14px; }
  .formula strong { font-size: 16px; }
  .pane-title h2 { font-size: 16px; }
  .entry-pane .kind-button { font-size: 14px; padding: 6px 6px; }
  .entered-name { font-size: 15px; }
  .entered-qty input { font-size: 22px; }
}

@media (orientation: portrait) and (max-width: 420px) {
  h1 { font-size: 22px; }
  .total-card strong { font-size: 34px; }
  .total-card .sho { font-size: 26px; }
  .total-card em { font-size: 16px; }
  .admin-gate h2 { font-size: 15.84px; }
  .split { gap: 8px; }
}

.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  align-items: stretch;
  min-height: 0;
}

.pane {
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 12px;
}

.pane-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.pane-title h2 {
  font-size: 18px;
  color: var(--green-deep);
}

.entry-status {
  min-height: 1.4em;
  margin: 0 0 8px;
  color: var(--green);
  font-weight: 700;
  font-size: 15px;
}

.entry-pane .leftover {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 12px;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.entry-pane .row,
.entry-pane .row.no-qty {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.entry-pane .row > .clear-row {
  width: 100%;
  min-height: 48px;
  border: 1px solid #e4b3c0;
  background: #fff;
}

.entry-pane .kind {
  flex-wrap: nowrap;
}

.entry-pane .kind-button {
  flex: 1 1 0;
  min-width: 0;
  min-height: 42px;
  padding: 6px 2px;
  font-size: 14px;
  text-align: center;
}

.entry-pane .row.is-idle .per-stat,
.entry-pane .row.is-idle > .field,
.entry-pane .row.is-idle .line,
.entry-pane .row.is-idle .clear-row {
  display: none;
}

.entry-pane input,
.entry-pane select {
  min-height: 48px;
  padding: 8px 30px 8px 10px;
  font-size: 16px;
}

.commit-row { width: 100%; }
.commit-row[hidden] { display: none; }

.entered-pane {
  display: flex;
  flex-direction: column;
  align-self: start;
  width: 100%;
  background: #f7fbf8;
}

.entered-pane.has-new {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(31, 77, 58, 0.2);
}

.entered-count {
  flex: 0 0 auto;
  background: var(--green);
  color: #fff;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.entered-empty {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 15px;
}

.entered-empty[hidden] { display: none; }

.entered-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.entered-item {
  border: 1px solid #b7cfc2;
  border-left: 6px solid var(--green);
  border-radius: 14px;
  padding: 10px 10px 8px;
  background: #fff;
}

.entered-item.is-new {
  animation: entered-pop 0.7s ease;
}

@keyframes entered-pop {
  0% { background: #d7f0e1; }
  100% { background: #fff; }
}

.entered-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.entered-kicker {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.entered-remove {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--danger);
  font-weight: 700;
  min-height: 36px;
  padding: 4px 2px;
}

.entered-name {
  margin: 2px 0 8px;
  font-weight: 700;
  font-size: 17px;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.entered-qty {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--brown);
  font-size: 18px;
  font-weight: 700;
}

.entered-qty span {
  flex: 0 0 auto;
  white-space: nowrap;
}

.entered-qty input {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
  max-width: none;
  min-height: 48px;
  padding: 6px 8px;
  font-size: 28px;
  font-weight: 700;
  color: var(--brown);
  text-align: center;
}

.entered-line,
.entered-fixed {
  margin: 6px 0 0;
  text-align: right;
  color: var(--brown);
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.formula {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 8px;
  margin: 14px 2px 0;
  color: var(--muted);
  font-size: 15px;
}

.formula > span {
  white-space: nowrap;
}

.formula .op {
  min-height: 0;
  font-size: 18px;
}

.formula strong {
  color: var(--brown);
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

@media (prefers-reduced-motion: reduce) {
  .entered-item.is-new { animation: none; background: #e7f6ec; }
}
