:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --fg: #111827;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-fg: #ffffff;
  --primary-soft: #ccfbf1;
  --success: #16a34a;
  --warning: #b45309;
  --warning-bg: #fef3c7;
  --danger: #dc2626;
  --shadow: 0 2px 8px rgba(15, 23, 42, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --card: #1e293b;
    --fg: #f8fafc;
    --muted: #94a3b8;
    --border: #334155;
    --primary: #14b8a6;
    --primary-fg: #042f2e;
    --primary-soft: rgba(20, 184, 166, 0.16);
    --success: #22c55e;
    --warning: #fbbf24;
    --warning-bg: rgba(251, 191, 36, 0.14);
    --danger: #ef4444;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

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

.app-shell {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(74px + env(safe-area-inset-bottom));
}

/* Sticky Slim Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: calc(6px + env(safe-area-inset-top)) 12px 7px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  box-shadow: var(--shadow);
}

.header-row1 {
  display: flex;
  align-items: center;
  gap: 6px;
}

.week-trigger {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  cursor: pointer;
  text-align: left;
}
.week-trigger:active { transform: scale(0.985); }

.week-title {
  font-size: 15px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ver-pill {
  flex-shrink: 0;
  padding: 3px 8px;
  border-radius: 8px;
  background: var(--primary);
  color: var(--primary-fg);
  font-size: 12px;
  font-weight: 800;
}

.caret {
  margin-left: auto;
  font-size: 12px;
  color: var(--muted);
}

.sync-pill {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 9px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}
.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--success);
}
.sync-pill.syncing .sync-dot {
  background: var(--warning);
  animation: pulse 0.9s ease-in-out infinite;
}
.sync-pill.offline .sync-dot {
  background: var(--danger);
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.header-row2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 0 2px;
}

.remain-wrap {
  display: flex;
  align-items: baseline;
  gap: 5px;
}
.remain-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}
.remain-num {
  font-size: 19px;
  font-weight: 900;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.progress-track {
  flex: 1;
  max-width: 210px;
  height: 7px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--success);
  transition: width 0.25s ease;
  width: 0%;
}

/* Main content */
.app-main {
  flex: 1;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* 1-line stock launcher */
.stock-launcher {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}
.stock-launcher:active { transform: scale(0.985); }

.stock-badge {
  font-size: 12px;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
}
.stock-badge.active {
  background: var(--primary);
  color: var(--primary-fg);
}

/* Category Section (Apple Notes high-density style) */
.cat-section {
  display: flex;
  flex-direction: column;
  gap: 0;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  overflow: hidden;
}

.cat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.cat-title {
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
}
.cat-count {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
}
.cat-list {
  display: flex;
  flex-direction: column;
}

/* Big Supermarket Item Card */
.item-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 10px 11px 12px;
  border-radius: 14px;
  background: var(--card);
  border: 1.5px solid var(--border);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.15s ease;
  user-select: none;
}
.item-card:active {
  transform: scale(0.98);
  border-color: var(--primary);
}

.check-circle {
  width: 22px;
  height: 22px;
  border-radius: 999px;
  border: 2px solid var(--muted);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 900;
}

.item-body {
  flex: 1;
  min-width: 0;
}

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

.item-name {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
  word-break: break-word;
}

.item-qty {
  flex-shrink: 0;
  padding: 3px 8px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 800;
  color: var(--primary);
  max-width: 48%;
  text-align: right;
  line-height: 1.25;
}

.item-note {
  margin-top: 4px;
  font-size: 12px;
  font-weight: 700;
  color: var(--warning);
  line-height: 1.35;
}

.more-btn {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 800;
  color: var(--muted);
  cursor: pointer;
}
.more-btn:active { background: var(--border); }

/* Quick Extra Input */
.extra-box {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-radius: 18px;
  background: var(--card);
  border: 1.5px solid var(--border);
  box-shadow: var(--shadow);
}
.extra-input {
  flex: 1;
  min-width: 0;
  padding: 11px 12px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 16px;
  font-weight: 600;
  outline: none;
}
.extra-input:focus { border-color: var(--primary); }
.btn-primary {
  padding: 11px 16px;
  border-radius: 12px;
  border: none;
  background: var(--primary);
  color: var(--primary-fg);
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
  flex-shrink: 0;
}
.btn-primary:active { opacity: 0.85; }

/* Done / Skipped Accordion */
.done-box {
  border-radius: 18px;
  background: var(--card);
  border: 1px solid var(--border);
  overflow: hidden;
}
.done-toggle {
  width: 100%;
  padding: 11px 14px;
  background: transparent;
  border: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 800;
  color: var(--muted);
  cursor: pointer;
}
.done-list {
  padding: 4px 12px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.done-row {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 13px;
  background: var(--bg);
  border: none;
  font-size: 14px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
  opacity: 0.72;
}
.done-row .strike { text-decoration: line-through; }
.done-row .undo {
  flex-shrink: 0;
  font-size: 13px;
  font-weight: 800;
  color: var(--primary);
}

/* Complete button */
.complete-btn {
  width: 100%;
  padding: 13px;
  border-radius: 14px;
  border: none;
  background: var(--success);
  color: #ffffff;
  font-size: 15px;
  font-weight: 900;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.complete-btn:active { opacity: 0.88; }

.empty-done-card {
  padding: 28px 18px;
  border-radius: 20px;
  background: var(--card);
  border: 1.5px solid var(--border);
  text-align: center;
}

/* Menu Tab Cards */
.menu-card {
  padding: 16px;
  border-radius: 18px;
  background: var(--card);
  border: 1.5px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 7px;
  cursor: pointer;
}
.menu-card.dimmed { opacity: 0.45; }
.menu-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.day-badge {
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--primary);
  color: var(--primary-fg);
  font-size: 13px;
  font-weight: 900;
}
.nutr-badge {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}
.menu-main {
  font-size: 15px;
  font-weight: 800;
  line-height: 1.35;
}
.menu-sub {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}
.menu-ings {
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Settings & History */
.section-heading {
  font-size: 14px;
  font-weight: 900;
  margin: 4px 0 8px 4px;
}
.order-row, .history-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--card);
  border: 1px solid var(--border);
}
.order-btns {
  display: flex;
  gap: 6px;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 17px;
  font-weight: 900;
  cursor: pointer;
}
.icon-btn:disabled { opacity: 0.25; cursor: default; }

.action-card-btn {
  width: 100%;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--card);
  border: 1.5px solid var(--border);
  font-size: 14px;
  font-weight: 800;
  text-align: left;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  text-decoration: none;
  color: var(--fg);
}

/* Fixed Bottom Nav (3 tabs) */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 25;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 6px 10px calc(6px + env(safe-area-inset-bottom));
}
.bottom-nav-inner {
  max-width: 520px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 7px 4px;
  border-radius: 14px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.nav-btn.active {
  background: var(--primary-soft);
  color: var(--primary);
}
.nav-icon { font-size: 20px; line-height: 1.1; }
.nav-label { font-size: 12px; font-weight: 800; }

/* Bottom Sheet */
.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-overlay.hidden { display: none; }

.sheet-panel {
  width: 100%;
  max-width: 520px;
  max-height: 85dvh;
  overflow-y: auto;
  background: var(--card);
  border-radius: 26px 26px 0 0;
  border-top: 1px solid var(--border);
  padding: 18px 18px calc(22px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: slideUp 0.18s ease-out;
}
@keyframes slideUp {
  from { transform: translateY(28px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}
.sheet-close {
  padding: 8px 14px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}

.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

.choice-btn {
  padding: 10px 6px;
  border-radius: 14px;
  border: 2px solid var(--border);
  background: var(--bg);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.choice-btn.active {
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
}

.sheet-block {
  padding: 14px;
  border-radius: 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.full-btn {
  width: 100%;
  padding: 13px;
  border-radius: 16px;
  border: 2px solid var(--border);
  background: var(--bg);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.full-btn.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #ffffff;
}
.full-btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-fg);
}

/* Toast */
.toast {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 16px;
  background: var(--primary);
  color: var(--primary-fg);
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  text-align: center;
}
.toast.hidden { display: none; }

/* Lock / Key Setup Screen */
.lock-screen {
  max-width: 420px;
  margin: 48px auto;
  padding: 24px;
  border-radius: 24px;
  background: var(--card);
  border: 1.5px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}

/* Item Accordion & Check Hit Area (High-density 1-line compact style) */
.item-wrap {
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.item-wrap:last-child {
  border-bottom: none;
}
.item-wrap .item-card.compact {
  display: flex;
  align-items: center;
  min-height: 38px;
  padding: 0 4px 0 0;
  gap: 2px;
  border: none;
  border-radius: 0;
  box-shadow: none;
  background: transparent;
  cursor: default;
}
.item-wrap .item-card.compact:active {
  transform: none;
}
.check-hit {
  width: 42px;
  min-height: 38px;
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  background: transparent;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
}
.check-hit:active .check-circle {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.item-card.compact .check-circle {
  width: 20px;
  height: 20px;
  border-width: 1.75px;
}
.item-card.compact .item-body {
  flex: 1;
  min-width: 0;
  padding: 7px 4px 7px 0;
  cursor: pointer;
}
.item-line {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.35;
}
.item-card.compact .item-name {
  font-size: 15px;
  font-weight: 600;
}
.item-meta {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--muted);
}
.extra-tag {
  font-size: 11px;
  font-weight: 800;
  padding: 1px 5px;
  margin-left: 5px;
  border-radius: 5px;
  background: var(--primary-soft);
  color: var(--primary);
  vertical-align: middle;
}
.expand-chevron {
  flex-shrink: 0;
  width: 26px;
  height: 34px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.expand-chevron.open {
  color: var(--primary);
}
.more-btn.compact {
  width: 26px;
  height: 34px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 14px;
  color: var(--muted);
}
.item-accordion {
  padding: 10px 12px 12px;
  border-top: 1px dashed var(--border);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
}
.sub-badge {
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--warning-bg);
  color: var(--warning);
  font-size: 13px;
  font-weight: 800;
  line-height: 1.4;
}
.usage-heading {
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
}
.usage-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.usage-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--border);
}
.usage-text {
  font-size: 13px;
  font-weight: 700;
  line-height: 1.35;
  flex: 1;
  min-width: 0;
}
.usage-empty {
  font-size: 12px;
  color: var(--muted);
}
.photo-btn {
  flex-shrink: 0;
  padding: 5px 9px;
  border-radius: 8px;
  border: 1px solid var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  white-space: nowrap;
}
.photo-btn:active { opacity: 0.8; }
.photo-btn.disabled {
  border-color: var(--border);
  background: var(--bg);
  color: var(--muted);
  cursor: default;
  opacity: 0.7;
}
.menu-card-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 2px;
}

/* Recipe Photo Lightbox Modal */
.photo-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(15, 23, 42, 0.94);
  display: flex;
  flex-direction: column;
}
.photo-modal.hidden { display: none; }
.photo-modal-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: rgba(15, 23, 42, 0.96);
  color: #f8fafc;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}
.photo-modal-title {
  font-size: 14px;
  font-weight: 800;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.photo-modal-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.photo-bar-btn {
  padding: 7px 11px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.12);
  color: #ffffff;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}
.photo-bar-btn.close {
  background: var(--primary);
  border-color: var(--primary);
}
.photo-modal-stage {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  touch-action: pinch-zoom pan-x pan-y;
  -webkit-overflow-scrolling: touch;
}
.photo-modal-stage.zoomed {
  align-items: flex-start;
  justify-content: flex-start;
}
.photo-modal-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
  transition: width 0.2s ease;
}
.photo-modal-stage.zoomed .photo-modal-img {
  max-width: none;
  max-height: none;
  width: 180%;
}
.photo-modal-img.hidden,
.photo-spinner.hidden,
.photo-fallback.hidden {
  display: none;
}
.photo-spinner {
  color: #e2e8f0;
  font-size: 14px;
  font-weight: 800;
  padding: 14px 20px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
}
.photo-fallback {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: #f8fafc;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
}
