:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #0f766e;
  --primary-dark: #115e59;
  --accent: #2563eb;
  --danger: #dc2626;
  --warning-bg: #fffbeb;
  --todo-border: #f59e0b;
  --todo-bg: #fef3c7;
  --border: #e5e7eb;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; overflow-x: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }
input, select { font: inherit; max-width: 100%; min-width: 0; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 15px;
}
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-secondary { background: #eef2ff; border-color: #c7d2fe; }
.btn-ghost { background: transparent; }
.btn-ghost-light { background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.4); }
.btn-small { padding: 6px 10px; font-size: 13px; }
.btn-block { display: block; width: 100%; margin-top: 10px; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ---------- auth ---------- */
.auth-view { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth-card { background: var(--card); border-radius: 20px; padding: 28px 22px; width: 100%; max-width: 380px; box-shadow: 0 8px 30px rgba(0,0,0,.08); text-align: center; }
.auth-logo { font-size: 52px; }
.auth-card h1 { font-size: 22px; margin: 8px 0 4px; }
.auth-sub { color: var(--muted); margin: 0 0 18px; font-size: 14px; }
.auth-form { text-align: left; display: flex; flex-direction: column; gap: 12px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-form input { padding: 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 16px; color: var(--text); }
.warn-box { background: var(--warning-bg); border: 1px solid #fcd34d; color: #92400e; border-radius: 10px; padding: 10px; font-size: 13px; margin-bottom: 14px; text-align: left; }
.error-text { color: var(--danger); font-size: 13px; margin: 4px 0; }
.info-text { color: var(--primary); font-size: 13px; margin: 4px 0; }

/* ---------- layout ---------- */
.app-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px 4px; max-width: 640px; margin: 0 auto; }
.app-header h1 { font-size: 19px; margin: 0; }
.tabs { display: flex; gap: 8px; padding: 8px 16px; max-width: 640px; margin: 0 auto; }
.tab { flex: 1; padding: 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--card); font-size: 15px; }
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
main { max-width: 640px; margin: 0 auto; padding: 4px 16px 110px; }

.alert-chip { background: var(--todo-bg); border: 2px solid var(--todo-border); color: #92400e; border-radius: 999px; padding: 8px 14px; font-weight: 700; text-align: center; margin: 6px 0 10px; }
.chip-row { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; -webkit-overflow-scrolling: touch; }
.chip { white-space: nowrap; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--card); font-size: 13px; }
.chip.active { background: var(--text); color: #fff; border-color: var(--text); }

.card-list { display: flex; flex-direction: column; gap: 10px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.card.todo-highlight { background: var(--todo-bg); border: 3px solid var(--todo-border); }
.card-main { display: flex; gap: 10px; align-items: flex-start; }
.card-info { flex: 1; min-width: 0; }
.card-name { font-weight: 700; font-size: 16px; word-break: break-word; }
.card-meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
.expiry { font-size: 14px; margin-top: 2px; }
.expiry.expired { color: var(--danger); font-weight: 700; }
.expiry.soon { color: #d97706; font-weight: 700; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; border-radius: 6px; padding: 2px 6px; vertical-align: middle; }
.badge-todo { background: var(--todo-border); color: #fff; }
.badge-opened { background: #e0e7ff; color: #3730a3; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; align-items: center; }
.qty-ctrl { display: inline-flex; align-items: center; gap: 2px; }
.qty-ctrl button { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); font-size: 18px; }
.qty-ctrl span { min-width: 34px; text-align: center; font-weight: 700; }
.empty-text { color: var(--muted); text-align: center; padding: 24px 0; }

.thumb { width: 44px; height: 44px; border-radius: 10px; object-fit: cover; background: #f3f4f6; border: 1px solid var(--border); flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 20px; padding: 0; }
.thumb-large { width: 72px; height: 72px; font-size: 30px; }
button.thumb { cursor: zoom-in; }
div.thumb { cursor: default; }

.fab-row { position: fixed; left: 0; right: 0; bottom: 0; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: rgba(246,247,249,.95); display: flex; gap: 8px; max-width: 640px; margin: 0 auto; }
.fab-main { flex: 1.4; }
.fab-row .btn { flex: 1; padding: 14px 8px; font-size: 15px; }

/* ---------- todo ---------- */
.todo-actions { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 6px 0 10px; flex-wrap: wrap; }
.todo-manual { display: flex; gap: 6px; margin-bottom: 12px; }
.todo-manual input[type="text"] { flex: 1; padding: 10px; border: 1px solid var(--border); border-radius: 10px; font-size: 16px; }
.todo-manual input[type="number"] { width: 58px; padding: 10px 6px; border: 1px solid var(--border); border-radius: 10px; font-size: 16px; text-align: center; }
.todo-card { display: flex; gap: 10px; align-items: flex-start; }
.todo-check { width: 26px; height: 26px; margin-top: 8px; flex-shrink: 0; accent-color: var(--primary); }
.todo-done .card-name { text-decoration: line-through; color: var(--muted); }

/* ---------- scan ---------- */
.scan-view { position: fixed; inset: 0; background: #111827; color: #fff; z-index: 40; padding: 14px 16px calc(14px + env(safe-area-inset-bottom)); overflow-y: auto; }
.scan-topbar { display: flex; align-items: center; gap: 12px; font-weight: 700; margin-bottom: 10px; }
.scan-banner { text-align: center; font-size: 19px; font-weight: 800; padding: 12px; border-radius: 12px; margin-bottom: 10px; background: #374151; }
.scan-banner.state-starting { background: #374151; }
.scan-banner.state-searching { background: #1d4ed8; }
.scan-banner.state-detected { background: #d97706; }
.scan-banner.state-success { background: #059669; }
.scan-banner.state-error { background: var(--danger); }
.scan-frame { position: relative; border: 4px solid #374151; border-radius: 16px; overflow: hidden; aspect-ratio: 4 / 3; background: #000; touch-action: none; }
.scan-frame.state-starting { border-color: #374151; }
.scan-frame.state-searching { border-color: #3b82f6; }
.scan-frame.state-detected { border-color: #f59e0b; }
.scan-frame.state-success { border-color: #10b981; }
.scan-frame.state-error { border-color: var(--danger); }
.scan-frame video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-guide { position: absolute; inset: 22% 12%; border: 2px dashed rgba(255,255,255,.75); border-radius: 10px; pointer-events: none; }
.scan-line { position: absolute; left: 12%; right: 12%; height: 3px; background: #f87171; top: 25%; animation: scanmove 1.6s ease-in-out infinite alternate; box-shadow: 0 0 10px #f87171; }
@keyframes scanmove { from { top: 25%; } to { top: 72%; } }
.scan-hint { text-align: center; color: #d1d5db; font-size: 14px; }
.scan-zoom { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.scan-zoom input { flex: 1; }
.scan-manual { display: flex; gap: 6px; margin-top: 10px; }
.scan-manual input { flex: 1; padding: 12px; border-radius: 10px; border: none; font-size: 16px; }
#scanRetryBtn { width: 100%; margin-top: 12px; }

/* ---------- form ---------- */
.form-view { position: fixed; inset: 0; background: var(--bg); z-index: 30; overflow-y: auto; padding: 12px 16px calc(20px + env(safe-area-inset-bottom)); }
.form-topbar { display: flex; align-items: center; gap: 10px; max-width: 560px; margin: 0 auto 10px; }
.form-topbar h2 { font-size: 18px; margin: 0; }
#itemForm { max-width: 560px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.form-photo-row { display: flex; gap: 12px; align-items: center; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.form-photo-btns { display: flex; flex-direction: column; gap: 6px; }
.hint-text { color: var(--muted); font-size: 12px; margin: 0; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 14px; min-width: 0; }
.field > input, .field > select { padding: 11px; border: 1px solid var(--border); border-radius: 10px; font-size: 16px; background: var(--card); width: 100%; }
.field-label { font-size: 14px; }
.req { color: var(--danger); font-size: 12px; }
.field-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check-field { justify-content: flex-start; }
.check-field input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--primary); }
.expiry-inputs { display: flex; align-items: center; gap: 6px; }
.expiry-inputs input { flex: 1; padding: 11px 6px; border: 1px solid var(--border); border-radius: 10px; font-size: 16px; text-align: center; background: var(--card); }
.expiry-inputs input:first-child { flex: 1.3; }
.segment { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segment-btn { flex: 1; padding: 11px; border: none; background: var(--card); font-size: 15px; }
.segment-btn.active { background: var(--primary); color: #fff; }
.jan-line { color: var(--muted); font-size: 13px; margin: 0; }

/* ---------- lightbox / modal / toast ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 60; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 20px; }
.lightbox img { max-width: 100%; max-height: 75dvh; border-radius: 12px; object-fit: contain; }
.lightbox p { color: #fff; font-size: 17px; font-weight: 700; margin: 0; text-align: center; }
.lightbox-close { position: absolute; top: calc(14px + env(safe-area-inset-top)); right: 16px; width: 42px; height: 42px; border-radius: 50%; border: none; background: rgba(255,255,255,.2); color: #fff; font-size: 18px; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 55; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-card { background: var(--card); border-radius: 16px; padding: 20px; width: 100%; max-width: 380px; }
.modal-card p { margin: 0 0 16px; font-size: 16px; }
.modal-btns { display: flex; flex-direction: column; gap: 8px; }
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #111827; color: #fff; padding: 11px 18px; border-radius: 999px; font-size: 14px; z-index: 70; box-shadow: 0 6px 20px rgba(0,0,0,.3); max-width: 90vw; text-align: center; }

@media (prefers-color-scheme: dark) {
  :root { --bg: #111827; --card: #1f2937; --text: #f3f4f6; --muted: #9ca3af; --border: #374151; --todo-bg: #451a03; }
  .auth-form input, .field > input, .field > select, .expiry-inputs input, .todo-manual input { background: var(--card); color: var(--text); }
  .thumb { background: #374151; }
  .segment-btn { background: var(--card); color: var(--text); }
  .qty-ctrl button { background: var(--card); color: var(--text); }
}
