@layer reset, base, components, responsive;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; }
  body, h1, h2, h3, p, fieldset { margin: 0; }
  button, input, select, textarea { font: inherit; }
  button { cursor: pointer; }
  [hidden] { display: none !important; }
}

@layer base {
  :root {
    color-scheme: light;
    --bg: oklch(1 0 0);
    --surface: oklch(0.965 0.006 340);
    --surface-strong: oklch(0.925 0.012 340);
    --ink: oklch(0.205 0.025 340);
    --muted: oklch(0.47 0.025 340);
    --primary: oklch(0.36 0.147 340);
    --primary-hover: oklch(0.31 0.14 340);
    --primary-soft: oklch(0.94 0.035 340);
    --accent: oklch(0.78 0.15 155);
    --success: oklch(0.44 0.12 150);
    --success-soft: oklch(0.95 0.035 150);
    --danger: oklch(0.50 0.18 25);
    --danger-soft: oklch(0.955 0.03 25);
    --border: oklch(0.875 0.012 340);
    --focus: oklch(0.66 0.18 340);
    --white: oklch(1 0 0);
    --shadow: 0 16px 38px oklch(0.205 0.025 340 / 0.08);
    --radius-sm: 0.65rem;
    --radius: 0.9rem;
    --radius-lg: 1.25rem;
    --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  }

  body {
    min-height: 100vh;
    background: var(--surface);
    color: var(--ink);
    font-family: var(--font);
    font-size: 1rem;
    line-height: 1.5;
  }

  h1, h2, h3 { line-height: 1.2; text-wrap: balance; }
  h1 { font-size: 1.75rem; letter-spacing: -0.025em; }
  h2 { font-size: 1.2rem; }
  h3 { font-size: 1rem; }
  a { color: var(--primary); }

  :focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 3px;
  }

  .page {
    width: min(100% - 2rem, 42rem);
    margin-inline: auto;
    padding-block: 2rem 3rem;
  }
}

@layer components {
  .page-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.5rem;
  }

  .menu-photo {
    overflow: hidden;
    margin: 0 0 1.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg);
    box-shadow: var(--shadow);
  }

  .menu-photo img {
    display: block;
    width: 100%;
    height: auto;
    max-height: 26rem;
    object-fit: contain;
  }

  .menu-photo-button {
    display: block;
    width: 100%;
    border: 0;
    background: transparent;
    padding: 0;
    cursor: zoom-in;
  }

  .menu-photo-button:focus-visible {
    outline-offset: -4px;
  }

  .menu-photo figcaption {
    padding: 0.55rem 0.8rem;
    color: var(--muted);
    font-size: 0.85rem;
    text-align: center;
  }

  .menu-photo-dialog {
    width: 100vw;
    max-width: none;
    height: 100dvh;
    max-height: none;
    border: 0;
    background: transparent;
    padding: clamp(0.5rem, 2vw, 1.5rem);
  }

  .menu-photo-dialog::backdrop { background: oklch(0.12 0.02 340 / 0.88); }
  .menu-photo-dialog-content {
    display: flex;
    width: 100%;
    height: 100%;
    flex-direction: column;
    border-radius: var(--radius);
    background: var(--bg);
    overflow: hidden;
  }
  .menu-photo-dialog header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.75rem 1rem;
  }
  .menu-photo-dialog h2 { font-size: 1.1rem; }
  .dialog-close, .dialog-control {
    min-height: 2.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    padding: 0.55rem 0.8rem;
    color: var(--ink);
    font-weight: 750;
  }
  .menu-photo-controls {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    border-block: 1px solid var(--border);
    background: var(--surface);
    padding: 0.55rem 0.75rem;
  }
  .dialog-control {
    min-width: 2.75rem;
    padding: 0.4rem 0.7rem;
    font-size: 1.15rem;
  }
  .dialog-control:disabled { cursor: not-allowed; opacity: 0.45; }
  .reset-control { font-size: 0.9rem; }
  #menu-photo-zoom-level {
    min-width: 3.5rem;
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    font-weight: 750;
    text-align: center;
  }
  .menu-photo-viewport {
    flex: 1;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    background: var(--surface-strong);
    touch-action: pan-x pan-y pinch-zoom;
    -webkit-overflow-scrolling: touch;
  }
  .menu-photo-viewport img {
    display: block;
    width: 100%;
    min-width: 0;
    max-width: none;
    height: auto;
    min-height: 0;
    margin-inline: auto;
    object-fit: contain;
  }

  .page-header p, .admin-header p, .section-heading p, .privacy-note {
    margin-top: 0.3rem;
    color: var(--muted);
  }

  .brand-mark {
    display: grid;
    flex: 0 0 auto;
    width: 3.25rem;
    height: 3.25rem;
    place-items: center;
    border-radius: 1rem;
    background: var(--primary);
    color: var(--white);
    font-size: 1.25rem;
    font-weight: 800;
    box-shadow: 0 8px 20px oklch(0.36 0.147 340 / 0.2);
  }

  .order-form {
    display: grid;
    gap: 1.4rem;
    padding: 1.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg);
    box-shadow: var(--shadow);
  }

  .field-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 7rem;
    gap: 1rem;
  }

  .field {
    display: grid;
    gap: 0.5rem;
  }

  .field > span, .choice-group legend {
    font-size: 0.93rem;
    font-weight: 700;
  }

  .field b, .choice-group b { color: var(--danger); }
  .field em, .choice-group em { color: var(--muted); font-size: 0.82rem; font-style: normal; font-weight: 600; }

  .drink-info {
    display: grid;
    gap: 0.35rem;
    margin-top: -0.8rem;
    border-left: 4px solid var(--primary);
    border-radius: var(--radius-sm);
    background: var(--surface);
    padding: 0.8rem 1rem;
  }
  .drink-info[hidden] { display: none; }
  .drink-info h2 { font-size: 0.93rem; }
  .drink-info p { color: var(--muted); }
  .drink-notice {
    border-radius: var(--radius-sm);
    background: oklch(0.94 0.06 70);
    padding: 0.65rem 0.75rem;
    color: oklch(0.34 0.1 45) !important;
    font-weight: 750;
  }

  input:not([type="radio"]):not([type="checkbox"]), select, textarea {
    width: 100%;
    min-height: 3rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--ink);
    padding: 0.72rem 0.85rem;
    transition: border-color 180ms ease, box-shadow 180ms ease;
  }

  textarea { resize: vertical; }
  input::placeholder, textarea::placeholder { color: oklch(0.48 0.018 340); opacity: 1; }
  input:not([type="radio"]):not([type="checkbox"]):hover, select:hover, textarea:hover { border-color: oklch(0.68 0.035 340); }
  input:not([type="radio"]):not([type="checkbox"]):focus, select:focus, textarea:focus {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 3px oklch(0.66 0.18 340 / 0.2);
  }

  .choice-group {
    min-width: 0;
    padding: 0;
    border: 0;
  }

  .choice-group legend { margin-bottom: 0.6rem; }

  .choices {
    display: flex;
    flex-wrap: wrap;
    gap: 0.55rem;
  }

  .choices label { position: relative; }
  .choices input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
  }

  .choices span {
    display: grid;
    gap: 0.2rem;
    min-height: 2.75rem;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg);
    padding: 0.55rem 0.9rem;
    color: var(--ink);
    font-size: 0.93rem;
    font-weight: 650;
    transition: background-color 180ms ease, border-color 180ms ease, color 180ms ease;
    cursor: pointer;
  }

  .choices span:hover { border-color: oklch(0.63 0.08 340); }
  .choices input:checked + span { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
  .choices input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 3px; }
  .choice-hint { color: var(--muted); font-size: 0.9rem; }
  .fixed-size {
    min-height: 2.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    padding: 0.65rem 0.9rem;
    font-weight: 700;
  }
  .topping-choices span { border-radius: var(--radius-sm); }
  .choice-price {
    display: block;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
  }

  .price-summary {
    display: grid;
    gap: 0.8rem;
    border: 2px solid var(--primary);
    border-radius: var(--radius);
    background: var(--primary-soft);
    padding: 1rem;
  }
  .price-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
  .price-heading strong { color: var(--primary); font-size: 1.35rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .price-summary dl { display: grid; gap: 0.35rem; margin: 0; }
  .price-summary dl div { display: flex; justify-content: space-between; gap: 1rem; }
  .price-summary dt { color: var(--muted); }
  .price-summary dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 700; }

  .primary-button, .secondary-button {
    display: inline-grid;
    min-height: 3rem;
    place-items: center;
    border-radius: var(--radius-sm);
    padding: 0.7rem 1rem;
    font-weight: 750;
    text-decoration: none;
    transition: background-color 180ms ease, border-color 180ms ease, transform 180ms ease;
  }

  .primary-button {
    width: 100%;
    border: 1px solid var(--primary);
    background: var(--primary);
    color: var(--white);
  }

  .primary-button:hover { border-color: var(--primary-hover); background: var(--primary-hover); }
  .primary-button:active, .secondary-button:active { transform: translateY(1px); }
  button:disabled { cursor: wait; opacity: 0.65; }

  .secondary-button {
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--ink);
  }

  .secondary-button:hover { border-color: var(--primary); color: var(--primary); }

  .form-message {
    border: 1px solid currentColor;
    border-radius: var(--radius-sm);
    padding: 0.8rem 0.9rem;
    font-weight: 650;
  }

  .form-message.success { background: var(--success-soft); color: var(--success); }
  .form-message.error { background: var(--danger-soft); color: var(--danger); }
  #admin-error, #action-feedback { margin-bottom: 1rem; }
  .privacy-note { text-align: center; font-size: 0.85rem; }

  .admin-page { width: min(100% - 2rem, 78rem); }
  .admin-header, .section-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 1rem;
  }

  .admin-header { margin-bottom: 1.5rem; }
  .admin-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 0.65rem; }
  .export-button { color: var(--primary); font-weight: 800; }
  .section-label { color: var(--primary) !important; font-weight: 750; }

  .metrics {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    overflow: hidden;
    margin-bottom: 2rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg);
    box-shadow: var(--shadow);
  }

  .metrics > div { display: grid; gap: 0.1rem; padding: 1.2rem 1.4rem; }
  .metrics > div + div { border-left: 1px solid var(--border); }
  .metrics span { color: var(--primary); font-size: 1.8rem; font-variant-numeric: tabular-nums; font-weight: 800; }
  .metrics small { color: var(--muted); font-size: 0.85rem; font-weight: 650; }

  .summary-section, .orders-section {
    margin-top: 1.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg);
    padding: 1.4rem;
  }

  .breakdown-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 1.5rem;
    margin-top: 1.25rem;
  }

  .breakdown-panel + .breakdown-panel { border-left: 1px solid var(--border); padding-left: 1.5rem; }
  .breakdown-list { display: grid; gap: 0.85rem; margin-top: 0.9rem; }
  .breakdown-row > div:first-child { display: flex; justify-content: space-between; gap: 1rem; font-size: 0.9rem; }
  .breakdown-row strong { font-variant-numeric: tabular-nums; white-space: nowrap; }
  .bar-track { height: 0.38rem; overflow: hidden; margin-top: 0.35rem; border-radius: 999px; background: var(--surface-strong); }
  .bar-track span { display: block; height: 100%; border-radius: inherit; background: var(--primary); }
  .muted-row { color: var(--muted); font-size: 0.9rem; }

  .table-wrap { overflow-x: auto; margin-top: 1.1rem; }
  table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
  th, td { border-bottom: 1px solid var(--border); padding: 0.8rem 0.65rem; text-align: left; vertical-align: top; }
  th { color: var(--muted); font-size: 0.82rem; white-space: nowrap; }
  tbody tr:last-child td { border-bottom: 0; }
  tbody tr:hover { background: var(--surface); }
  .quantity-cell { color: var(--primary); font-weight: 750; white-space: nowrap; }
  .notes-cell { max-width: 20rem; }

  .row-actions {
    display: flex;
    min-width: 8.5rem;
    gap: 0.5rem;
  }

  .order-action, .icon-button {
    min-width: 3.75rem;
    min-height: 2.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--ink);
    padding: 0.55rem 0.75rem;
    font-weight: 750;
  }

  .edit-button:hover { border-color: var(--primary); color: var(--primary); }
  .delete-button { border-color: oklch(0.77 0.09 25); color: var(--danger); }
  .delete-button:hover { border-color: var(--danger); background: var(--danger-soft); }

  .edit-dialog {
    width: min(calc(100% - 2rem), 38rem);
    max-height: calc(100dvh - 2rem);
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg);
    color: var(--ink);
    padding: 0;
    box-shadow: 0 24px 64px oklch(0.205 0.025 340 / 0.24);
  }

  .edit-dialog::backdrop { background: oklch(0.205 0.025 340 / 0.55); }
  .edit-dialog form { display: grid; gap: 1.25rem; padding: 1.4rem; }
  .dialog-heading, .dialog-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
  }
  .icon-button {
    min-width: 2.75rem;
    padding: 0;
    color: var(--muted);
    font-size: 1.6rem;
    line-height: 1;
  }
  .icon-button:hover { border-color: var(--primary); color: var(--primary); }
  .dialog-fields { display: grid; gap: 1rem; }
  .field-warning { color: var(--danger); font-weight: 700; }
  .edit-price {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.15rem 1rem;
    border: 1px solid var(--primary);
    border-radius: var(--radius-sm);
    background: var(--primary-soft);
    padding: 0.9rem;
  }
  .edit-price > span { font-weight: 700; }
  .edit-price > strong { color: var(--primary); font-size: 1.2rem; white-space: nowrap; }
  .edit-price > small { grid-column: 1 / -1; color: var(--muted); }
  .dialog-actions { justify-content: flex-end; }
  .dialog-actions .primary-button { width: auto; }

  .empty-state {
    display: grid;
    gap: 0.25rem;
    place-items: center;
    margin-top: 1.2rem;
    padding: 2.5rem 1rem;
    border-radius: var(--radius);
    background: var(--surface);
    text-align: center;
  }

  .empty-state span { color: var(--muted); }
  .auth-error { display: grid; min-height: 100vh; align-content: center; justify-items: start; gap: 1rem; }
}

@layer responsive {
  @media (max-width: 760px) {
    .page { padding-top: 1.25rem; }
    .order-form { padding: 1.1rem; }
    .topping-choices {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      align-items: stretch;
    }
    .topping-choices label {
      display: grid;
      min-width: 0;
    }
    .topping-choices span {
      width: 100%;
      height: 100%;
      min-width: 0;
      overflow-wrap: anywhere;
    }
    .menu-photo-dialog {
      padding: 0.5rem 0.5rem max(4.75rem, env(safe-area-inset-bottom));
    }
    .menu-photo-dialog header { padding: 0.6rem 0.75rem; }
    .menu-photo-dialog h2 { font-size: 1rem; }
    .menu-photo-controls { padding: 0.45rem 0.55rem; }
    .metrics { grid-template-columns: repeat(2, 1fr); }
    .metrics > div:nth-child(3) { border-top: 1px solid var(--border); border-left: 0; }
    .metrics > div:nth-child(4) { border-top: 1px solid var(--border); }
    .breakdown-grid { grid-template-columns: 1fr; }
    .breakdown-panel + .breakdown-panel { border-left: 0; border-top: 1px solid var(--border); padding-top: 1.25rem; padding-left: 0; }

    table, thead, tbody, tr, td { display: block; }
    thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
    tbody { display: grid; gap: 0.75rem; }
    tr { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.6rem 0.8rem; }
    td { display: grid; grid-template-columns: 5rem minmax(0, 1fr); gap: 0.75rem; border-bottom: 1px solid var(--border); padding: 0.55rem 0; }
    td::before { content: attr(data-label); color: var(--muted); font-size: 0.82rem; font-weight: 700; }
    td:last-child { border-bottom: 0; }
    tbody tr:hover { background: var(--bg); }
    .action-cell { align-items: center; }
    .row-actions { min-width: 0; }
  }

  @media (max-width: 480px) {
    .field-row { grid-template-columns: 1fr; }
    .page-header { align-items: flex-start; }
    .admin-header { align-items: flex-start; }
    .admin-header .secondary-button { min-height: 2.7rem; padding-inline: 0.75rem; }
    .admin-actions { justify-content: flex-start; }
    .metrics > div { padding: 1rem 0.8rem; }
    .metrics span { font-size: 1.55rem; }
    .summary-section, .orders-section { padding: 1rem; }
    .section-heading { align-items: start; }
    .choices label { flex: 1 1 calc(50% - 0.55rem); }
    .dialog-choice-row { grid-template-columns: 1fr; }
    .edit-dialog form { padding: 1.1rem; }
    .dialog-actions > button { flex: 1 1 0; }
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
  }
}
