/*
 * 주간 식단표 스타일: 색 변수(라이트/다크), 헤더·보드 레이아웃(모바일 카드 → 태블릿 2열 → 데스크톱 7열 표),
 * 메뉴 항목, 버튼, dialog, 토스트. 다크모드는 prefers-color-scheme만 따른다.
 */

/* ---------- 변수 ---------- */
:root {
  --bg: #f5f6f2;
  --surface: #ffffff;
  --surface-2: #f0f3ed;
  --text: #1c231f;
  --muted: #56635b;
  --border: #dde3d9;
  --border-strong: #c5cec1;
  --accent: #2b7a4b;
  --accent-hover: #236640;
  --on-accent: #ffffff;
  --accent-soft: #e3f1e8;
  --accent-text: #1f6239;
  --danger: #b4321f;
  --danger-soft: #fbe9e6;
  --today: #fff8e3;
  --today-border: #e3b23c;
  --today-text: #7a5300;
  --yt: #d4201f;
  --focus: #1f6fd1;
  --shadow: 0 1px 2px rgb(20 30 25 / 0.06), 0 4px 16px rgb(20 30 25 / 0.06);
  --shadow-lg: 0 12px 40px rgb(20 30 25 / 0.18);
  --radius: 14px;
  --radius-sm: 10px;
  --header-h: 4.25rem;
  --font: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111412;
    --surface: #1a1f1c;
    --surface-2: #222925;
    --text: #e6ebe7;
    --muted: #a2afa7;
    --border: #2e3732;
    --border-strong: #3d4842;
    --accent: #5cc48a;
    --accent-hover: #74d29d;
    --on-accent: #0b1f13;
    --accent-soft: #1d3527;
    --accent-text: #86d7a8;
    --danger: #ff8a7a;
    --danger-soft: #3a1f1b;
    --today: #2a2516;
    --today-border: #c99a2e;
    --today-text: #f0c968;
    --yt: #ff5c5c;
    --focus: #7ab4ff;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 16px rgb(0 0 0 / 0.25);
    --shadow-lg: 0 12px 40px rgb(0 0 0 / 0.5);
  }
}

/* ---------- 기본 ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent-text); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 6px;
}

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  left: 1rem; top: -3rem;
  z-index: 100;
  padding: 0.5rem 1rem;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}
.skip-link:focus { top: 0.75rem; }

.icon {
  width: 1.15em;
  height: 1.15em;
  flex: none;
  display: block;
}
.icon .stroke { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon .fill { fill: currentColor; }
.icon .inner { fill: var(--surface); }

/* ---------- 버튼 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 40px;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn .btn-icon { display: inline-flex; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.8; }

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--surface);
}
.btn-danger:hover:not(:disabled) { background: var(--danger); filter: brightness(0.92); }

.btn-quiet-danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn-quiet-danger:hover:not(:disabled) { background: var(--danger-soft); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  text-decoration: none;
  flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn .icon { width: 18px; height: 18px; }
.icon-btn-danger:hover { background: var(--danger-soft); color: var(--danger); }
.link-video { color: var(--yt); }
.link-video .icon { width: 22px; height: 22px; }
.link-video:hover { color: var(--yt); }

.label-short { display: none; }

/* ---------- 스피너 ---------- */
.spinner {
  display: inline-block;
  width: 1.05em;
  height: 1.05em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.spinner-lg { width: 2.25rem; height: 2.25rem; border-width: 3px; color: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
  * { transition: none !important; }
}

/* ---------- 헤더 ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  max-width: 1480px;
  margin: 0 auto;
  padding: 0.75rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem 1rem;
}
.brand { display: flex; align-items: center; gap: 0.65rem; min-width: 0; margin-right: auto; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 2.5rem; height: 2.5rem;
  border-radius: 12px;
  background: var(--accent-soft);
  font-size: 1.35rem;
}
.brand h1 { font-size: 1.2rem; letter-spacing: -0.02em; line-height: 1.25; }
.week-range { color: var(--muted); font-size: 0.85rem; }
.toolbar { display: flex; gap: 0.5rem; }

/* ---------- 본문 ---------- */
.app-main {
  max-width: 1480px;
  margin: 0 auto;
  padding: 1rem 1rem 6rem;
}
.footnote { margin-top: 1rem; color: var(--muted); font-size: 0.8rem; text-align: center; }

/* 보드: 기본(모바일)은 세로 카드 스택 */
.week {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.week:focus { outline: none; }
.grid-corner, .row-label { display: none; }

.day {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  scroll-margin-top: calc(var(--header-h) + 1rem);
}
.day.is-today {
  border-color: var(--today-border);
  box-shadow: 0 0 0 1px var(--today-border), var(--shadow);
}
.day-head {
  display: flex;
  align-items: baseline;
  gap: 0.45rem;
  padding: 0.7rem 1rem;
  font-size: 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.day.is-today .day-head { background: var(--today); }
.day-name { font-weight: 800; font-size: 1.05rem; }
.day[data-day="sat"] .day-name { color: #2f63c4; }
.day[data-day="sun"] .day-name { color: #c43a2f; }
@media (prefers-color-scheme: dark) {
  .day[data-day="sat"] .day-name { color: #8fb3ff; }
  .day[data-day="sun"] .day-name { color: #ff9a8f; }
}
.day-date { color: var(--muted); font-size: 0.85rem; font-weight: 500; }
.today-badge {
  margin-left: auto;
  padding: 0.05rem 0.55rem;
  border-radius: 999px;
  background: var(--today-border);
  color: #2b1f00;
  font-size: 0.75rem;
  font-weight: 700;
  align-self: center;
}

.slot { padding: 0.75rem 1rem; display: flex; flex-direction: column; gap: 0.5rem; min-width: 0; }
.slot + .slot { border-top: 1px dashed var(--border); }
.slot-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.02em;
}
.slot-empty { color: var(--muted); font-size: 0.85rem; }

.items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.item {
  display: flex;
  align-items: flex-start;
  gap: 0.25rem;
  padding: 0.45rem 0.35rem 0.45rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.item-body { flex: 1; min-width: 0; padding-top: 0.35rem; }
.item-name {
  display: block;
  font-weight: 650;
  overflow-wrap: anywhere;
  cursor: pointer;
}
.item-name:hover { text-decoration: underline; text-underline-offset: 3px; }
.item-note, .item-nutrition { font-size: 0.8rem; color: var(--muted); overflow-wrap: anywhere; }
.item-nutrition { font-variant-numeric: tabular-nums; }
.item-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; flex: none; }

.add-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  min-height: 40px;
  padding: 0.3rem 0.75rem;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--accent-text);
  font-weight: 600;
  font-size: 0.875rem;
  cursor: pointer;
}
.add-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

.day-total {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem 0.5rem;
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--accent-soft);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}
.day-total.is-empty { display: none; }
.day-total-label { font-weight: 700; color: var(--accent-text); }

/* ---------- 태블릿: 요일 카드 2열 ---------- */
@media (min-width: 640px) and (max-width: 959.98px) {
  .week { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
}

/* ---------- 데스크톱: 행 라벨 1열 + 7열 표 ---------- */
@media (min-width: 960px) {
  .week {
    display: grid;
    grid-template-columns: 3.75rem repeat(7, minmax(0, 1fr));
    grid-template-rows: auto auto auto auto;
    gap: 0 0.5rem;
  }
  .grid-corner { display: block; grid-column: 1; grid-row: 1; }
  .row-label {
    display: flex;
    align-items: center;
    justify-content: center;
    grid-column: 1;
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--muted);
    writing-mode: vertical-rl;
    letter-spacing: 0.2em;
  }
  .row-breakfast { grid-row: 2; }
  .row-dinner { grid-row: 3; }
  .row-total { grid-row: 4; writing-mode: horizontal-tb; font-size: 0.72rem; letter-spacing: 0; text-align: center; white-space: pre-line; }

  .day {
    display: grid;
    grid-row: 1 / span 4;
    grid-template-rows: subgrid;
    row-gap: 0;
  }
  .day[data-day="mon"] { grid-column: 2; }
  .day[data-day="tue"] { grid-column: 3; }
  .day[data-day="wed"] { grid-column: 4; }
  .day[data-day="thu"] { grid-column: 5; }
  .day[data-day="fri"] { grid-column: 6; }
  .day[data-day="sat"] { grid-column: 7; }
  .day[data-day="sun"] { grid-column: 8; }

  .day-head { padding: 0.6rem 0.7rem; flex-wrap: wrap; }
  .today-badge { margin-left: 0; }
  .slot { padding: 0.6rem; }
  .slot-title,
  .day-total-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .day-total { display: block; padding: 0.55rem 0.7rem; }
  .day-total.is-empty { display: block; background: transparent; }

  .item { flex-direction: column; align-items: stretch; padding: 0.45rem 0.5rem; gap: 0.15rem; }
  .item-body { padding-top: 0; }
  .item-actions { justify-content: flex-start; margin-left: -0.4rem; }
  .add-btn { width: 100%; }
}

/* 마우스 환경의 좁은 데스크톱 칸에서는 아이콘 버튼을 조금 줄인다(터치 기기는 40px 유지). */
@media (min-width: 960px) and (pointer: fine) {
  .item .icon-btn { width: 32px; height: 32px; }
}

/* ---------- 모바일 헤더 ---------- */
@media (max-width: 639.98px) {
  :root { --header-h: 7rem; }
  .header-inner { padding: 0.6rem 0.75rem; }
  .app-main { padding: 0.75rem 0.75rem 6rem; }
  .toolbar { width: 100%; }
  .toolbar .btn { flex: 1; padding-inline: 0.5rem; }
  .label-long { display: none; }
  .label-short { display: inline; }
  .brand h1 { font-size: 1.1rem; }
}

/* ---------- AI 로딩 오버레이 ---------- */
.board-overlay {
  position: fixed;
  inset: 0;
  z-index: 15;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  backdrop-filter: blur(2px);
}
.overlay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  max-width: 22rem;
  padding: 1.5rem 1.75rem;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}
.overlay-title { font-weight: 700; }
.overlay-sub { color: var(--muted); font-size: 0.875rem; }

/* ---------- Dialog ---------- */
.dialog {
  width: min(34rem, calc(100vw - 1.5rem));
  max-height: calc(100dvh - 2rem);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
.dialog-sm { width: min(26rem, calc(100vw - 1.5rem)); }
.dialog::backdrop { background: rgb(10 15 12 / 0.45); backdrop-filter: blur(2px); }
.dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 2rem); }
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 0.75rem 0.5rem 1.25rem;
}
.dialog-header h2, .dialog-body > h2 { font-size: 1.1rem; letter-spacing: -0.01em; }
.dialog-body {
  padding: 0.5rem 1.25rem 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.dialog-sm .dialog-body { padding-top: 1.25rem; gap: 0.5rem; }
.confirm-message { color: var(--muted); overflow-wrap: anywhere; }
.dialog-footer {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.85rem 1.25rem 1.1rem;
  border-top: 1px solid var(--border);
}
.spacer { flex: 1; }

.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field label { font-weight: 600; font-size: 0.9rem; }
.required { color: var(--danger); }
.field input, .field textarea {
  width: 100%;
  min-height: 42px;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.field textarea { resize: vertical; }
.field input:focus-visible, .field textarea:focus-visible { outline-offset: 0; border-color: var(--focus); }
.field input[aria-invalid="true"], .field textarea[aria-invalid="true"] { border-color: var(--danger); }
.field-hint { color: var(--muted); font-size: 0.8rem; }
.field-error { color: var(--danger); font-size: 0.82rem; font-weight: 600; }
.field-error:empty { display: none; }
.input-row { display: flex; gap: 0.5rem; }
.input-row input { flex: 1; min-width: 0; }

.notice {
  padding: 0.7rem 0.85rem;
  border-radius: var(--radius-sm);
  background: var(--today);
  color: var(--text);
  font-size: 0.82rem;
  line-height: 1.55;
}

.nutrition-fields {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 0.85rem;
}
.nutrition-fields summary {
  min-height: 42px;
  display: flex;
  align-items: center;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.nutrition-fields[open] { padding-bottom: 0.85rem; }
.nutrition-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }

/* ---------- 토스트 ---------- */
.toast-region {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: max(1rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(26rem, calc(100vw - 1.5rem));
  pointer-events: none;
}
.toast-stack { display: flex; flex-direction: column; gap: 0.5rem; }
.toast-stack:not(:empty) + .toast-stack:not(:empty) { margin-top: 0.5rem; }
@media (min-width: 960px) {
  .toast-region { left: auto; right: 1.25rem; transform: none; }
}
.toast {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.4rem 0.45rem 1rem;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  cursor: pointer;
  animation: toast-in 0.18s ease-out;
}
.toast-error { background: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) {
  .toast-error { color: #2a0b06; }
}
.toast-message { flex: 1; font-size: 0.9rem; font-weight: 500; }
.toast-action {
  min-height: 40px;
  padding: 0.2rem 0.75rem;
  border: 0;
  border-radius: 8px;
  background: color-mix(in srgb, currentColor 18%, transparent);
  color: inherit;
  font-weight: 700;
  cursor: pointer;
}
.toast-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px; height: 40px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  cursor: pointer;
  opacity: 0.8;
}
.toast-close .icon { width: 16px; height: 16px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
}
