:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --text: #1c1f1c;
  --muted: #5d655e;
  --border: #d9ddd6;
  --primary: #1f5130;
  --primary-text: #ffffff;
  --primary-pressed: #173d24;
  --error: #a4262c;
  --error-bg: #fbeaea;
  --toast-bg: #1c1f1c;
  --toast-text: #ffffff;
  --radius: 12px;
  --tap: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121412;
    --surface: #1d201d;
    --text: #eef0ec;
    --muted: #a3aba4;
    --border: #333833;
    --primary: #4c9a64;
    --primary-text: #0b140e;
    --primary-pressed: #3d8153;
    --error: #ff8a8a;
    --error-bg: #3a1a1b;
    --toast-bg: #eef0ec;
    --toast-text: #121412;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 18px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

[hidden] { display: none !important; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* --- Layout ------------------------------------------------------------- */

.topbar {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: calc(12px + env(safe-area-inset-top)) 20px 12px;
  background: var(--primary); color: var(--primary-text);
}
.topbar h1 { margin: 0; font-size: 22px; font-weight: 650; }
.env-badge {
  display: inline-block; margin-left: 8px; padding: 2px 10px; vertical-align: middle;
  font-size: 14px; font-weight: 700; letter-spacing: 0.02em;
  background: #f2c94c; color: #1c1f1c; border-radius: 999px;
}
.account { display: flex; align-items: center; gap: 12px; font-size: 16px; }
.account .btn-quiet { color: inherit; border-color: currentColor; }

main { max-width: 960px; margin: 0 auto; padding: 20px 16px 40px; }

.signin { text-align: center; padding: 64px 16px; }
.signin p { color: var(--muted); margin-bottom: 24px; }

.toolbar { display: flex; gap: 12px; margin-bottom: 8px; }
.search { flex: 1; }
.search input {
  width: 100%; height: var(--tap); padding: 0 16px;
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.category select {
  height: var(--tap); max-width: 240px; padding: 0 12px;
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}

@media (max-width: 600px) {
  .toolbar { flex-wrap: wrap; }
  .search { flex-basis: 100%; }
  .category { flex: 1; }
  .category select { width: 100%; max-width: none; }
}

.status { color: var(--muted); font-size: 15px; margin: 8px 4px 12px; }
.status-error {
  color: var(--error); background: var(--error-bg);
  padding: 12px 14px; border-radius: var(--radius); font-size: 16px;
}

/* --- Item list ---------------------------------------------------------- */

.item-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 12px 12px 18px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.item-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.item-no { font-weight: 650; font-size: 20px; font-variant-numeric: tabular-nums; }
.item-desc { color: var(--muted); font-size: 16px; overflow-wrap: anywhere; }
.item-category {
  align-self: flex-start; margin-top: 4px; padding: 2px 10px;
  font-size: 13px; font-weight: 600; color: var(--primary);
  border: 1px solid var(--primary); border-radius: 999px;
}

/* --- Buttons ------------------------------------------------------------ */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  font: inherit; font-weight: 600; border-radius: var(--radius);
  border: 1px solid transparent; cursor: pointer;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:active:not(:disabled) { background: var(--primary-pressed); }
.btn-quiet { background: transparent; color: var(--text); border-color: var(--border); }
.btn-large { min-height: var(--tap); padding: 0 28px; font-size: 19px; }
.btn-assemble { min-height: var(--tap); padding: 0 22px; flex-shrink: 0; }
.toolbar .btn-quiet { min-height: var(--tap); }

@media (max-width: 480px) {
  .btn-assemble span { display: none; }
  .btn-assemble { padding: 0 16px; }
}

/* --- Quantity dialog ---------------------------------------------------- */

dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 24px; border: none; border-radius: 16px;
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog h2 { margin: 0 0 8px; font-size: 24px; }
.dialog-item { margin: 0 0 20px; display: flex; flex-direction: column; }
.dialog-item span { color: var(--muted); }

.stepper { display: flex; gap: 12px; }
.stepper input {
  flex: 1; min-width: 0; height: 72px; text-align: center;
  font: inherit; font-size: 32px; font-weight: 650; font-variant-numeric: tabular-nums;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.btn-step {
  width: 72px; height: 72px; font-size: 32px;
  background: var(--bg); color: var(--text); border-color: var(--border);
}
.hint { color: var(--muted); font-size: 15px; margin: 10px 2px 0; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 12px; margin-top: 24px; }
.dialog-actions .btn { flex: 1; }

/* --- Log and toast ------------------------------------------------------ */

.log { margin-top: 32px; }
.log h2 { font-size: 17px; color: var(--muted); font-weight: 600; margin: 0 0 8px 4px; }
.log ul { list-style: none; margin: 0; padding: 0; font-variant-numeric: tabular-nums; }
.log li { padding: 8px 4px; border-bottom: 1px solid var(--border); white-space: pre; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%); z-index: 10;
  max-width: calc(100vw - 32px); padding: 14px 22px;
  background: var(--toast-bg); color: var(--toast-text);
  border-radius: var(--radius); font-weight: 600;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
}
