@layer reset, tokens, base, layout, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  body, h1, h2, p, dl, dd, ul, fieldset { margin: 0; }
  fieldset { padding: 0; border: 0; min-inline-size: 0; }
  ul { padding: 0; list-style: none; }
  input, select, button { font: inherit; color: inherit; }
}

@layer tokens {
  :root {
    color-scheme: light dark;
    --bg: light-dark(#f4f6fb, #0d1117);
    --surface: light-dark(#ffffff, #161b22);
    --surface-2: light-dark(#eef1f7, #1f2630);
    --text: light-dark(#14181f, #e6edf3);
    --muted: light-dark(#5b6472, #9aa6b2);
    --border: light-dark(#d9dee8, #30363d);
    --accent: light-dark(#1d4ed8, #6ea8fe);
    --accent-text: light-dark(#ffffff, #0d1117);
    --good: light-dark(#13795b, #3fb950);
    --warn: light-dark(#a15c00, #d29922);
    --bad: light-dark(#b42318, #f85149);

    --tone-cost: light-dark(#64748b, #8b98a9);
    --tone-ship: light-dark(#2563eb, #6ea8fe);
    --tone-fees: light-dark(#d97706, #e3a33b);
    --tone-profit: light-dark(#13795b, #3fb950);
    --tone-loss: light-dark(#b42318, #f85149);

    --radius: 0.9rem;
    --space: clamp(0.9rem, 0.6rem + 1.2vw, 1.5rem);
    --step-0: clamp(0.95rem, 0.9rem + 0.2vw, 1.05rem);
    --step-1: clamp(1.15rem, 1rem + 0.6vw, 1.4rem);
    --step-3: clamp(2.2rem, 1.6rem + 3vw, 3.4rem);
    --tabbar-h: 4rem;
    --tap: 2.75rem;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 6px 24px rgb(0 0 0 / 0.06);
  }
}

@layer base {
  html { -webkit-text-size-adjust: 100%; }
  body {
    min-block-size: 100dvh;
    background: var(--bg);
    color: var(--text);
    font: var(--step-0) / 1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    padding-inline: max(1rem, env(safe-area-inset-left));
    /* Mobile: leave room for the fixed tab bar + price peek. */
    padding-block-end: calc(var(--tabbar-h) + 4.5rem + env(safe-area-inset-bottom));
    @media (min-width: 52rem) { padding-block-end: 2rem; }
  }
  h1 { font-size: var(--step-1); line-height: 1.2; text-wrap: balance; }
  h2 { font-size: var(--step-0); margin-block: 1.5rem 0.5rem; }
  a { color: var(--accent); }
  small, .hint { color: var(--muted); }
  .hint { font-size: 0.875rem; }
  :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
}

@layer layout {
  .site-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    max-inline-size: 72rem;
    margin-inline: auto;
    padding-block: max(0.75rem, env(safe-area-inset-top)) 0.75rem;
  }

  /* Mobile: app-style bottom tab bar. */
  .tabbar {
    position: fixed;
    inset-inline: 0;
    inset-block-end: 0;
    z-index: 10;
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    padding-block-end: env(safe-area-inset-bottom);
    background: color-mix(in oklab, var(--surface) 92%, transparent);
    backdrop-filter: blur(12px);
    border-block-start: 1px solid var(--border);

    & a {
      display: grid;
      justify-items: center;
      align-content: center;
      gap: 0.15rem;
      min-block-size: var(--tabbar-h);
      color: var(--muted);
      font-size: 0.7rem;
      font-weight: 600;
      text-decoration: none;
      &[aria-current="page"] { color: var(--accent); }
    }
    & svg { inline-size: 1.4rem; block-size: 1.4rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  }

  /* Wide: tabs move into the header as pills. */
  @media (min-width: 52rem) {
    .site-header { justify-content: start; gap: 1.25rem; }
    .site-header .header-actions { margin-inline-start: auto; }
    .tabbar {
      position: static;
      display: flex;
      gap: 0.25rem;
      padding: 0;
      background: none;
      backdrop-filter: none;
      border: 0;
      & a {
        display: block;
        min-block-size: 0;
        padding: 0.4rem 0.8rem;
        border-radius: 999px;
        font-size: 0.95rem;
        font-weight: 500;
        &[aria-current="page"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
        &:hover { color: var(--text); }
      }
      & svg { display: none; }
    }
  }
  .brand {
    display: flex; align-items: center; gap: 0.5rem;
    font-weight: 700; color: var(--text); text-decoration: none;
  }
  main { max-inline-size: 72rem; margin-inline: auto; }

  .calc-layout {
    display: grid;
    gap: var(--space);
    align-items: start;
    @media (min-width: 52rem) { grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); }
  }
  .narrow { max-inline-size: 40rem; margin-inline: auto; }
}

@layer components {
  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: var(--space);
    display: grid;
    gap: 1rem;
    container-type: inline-size;
  }

  /* ---------- form controls ---------- */
  .field-grid {
    display: grid;
    gap: 0.75rem;
    grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
    &.dims { grid-template-columns: repeat(3, 1fr); margin-block-start: 0.75rem; }
  }
  .field {
    display: grid; gap: 0.3rem; align-content: start;
    & > span:first-child { font-size: 0.875rem; font-weight: 600; }
    &.align-end { align-content: end; }
    &.span-all { grid-column: 1 / -1; }
  }
  legend { font-size: 0.875rem; font-weight: 600; margin-block-end: 0.4rem; }
  input[type="number"], input[type="text"], select {
    inline-size: 100%;
    min-block-size: var(--tap);
    font-size: max(16px, 1rem);
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 0.55rem;
    background: var(--surface-2);
    &:focus { border-color: var(--accent); }
  }
  .affix {
    position: relative; display: block;
    &::before, &::after { position: absolute; inset-block: 0; display: grid; place-items: center; color: var(--muted); pointer-events: none; }
    &[data-prefix]::before { content: attr(data-prefix); inset-inline-start: 0.7rem; }
    &[data-prefix] input { padding-inline-start: 1.5rem; }
    &[data-suffix]::after { content: attr(data-suffix); inset-inline-end: 0.7rem; }
    &[data-suffix] input { padding-inline-end: 1.8rem; }
  }
  .segmented {
    display: flex; flex-wrap: wrap; gap: 0.4rem;
    & legend { inline-size: 100%; }
    & label {
      flex: 1 1 10rem;
      min-block-size: var(--tap);
      display: flex; gap: 0.5rem; align-items: center;
      padding: 0.55rem 0.8rem;
      border: 1px solid var(--border);
      border-radius: 0.55rem;
      cursor: pointer;
      &:has(:checked) { border-color: var(--accent); background: color-mix(in oklab, var(--accent) 10%, transparent); }
    }
    & input { accent-color: var(--accent); }
  }
  details summary {
    list-style: none;
    &::-webkit-details-marker { display: none; }
    &::before { content: "+"; display: inline-grid; place-items: center; inline-size: 1.2rem; block-size: 1.2rem; border-radius: 50%; background: var(--surface-2); font-weight: 700; }
    details[open] > &::before { content: "−"; }
    flex-wrap: wrap;
  }
  details summary { min-block-size: var(--tap); display: flex; align-items: center; gap: 0.4rem; cursor: pointer; font-size: 0.875rem; font-weight: 600; }
  button {
    min-block-size: var(--tap);
    padding: 0.6rem 1.1rem;
    border: 0;
    border-radius: 0.6rem;
    background: var(--accent);
    color: var(--accent-text);
    font-weight: 600;
    cursor: pointer;
    &.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
    &:disabled { opacity: 0.55; cursor: progress; }
    &.icon-btn { padding: 0.3rem 0.6rem; background: transparent; color: var(--muted); &:hover { color: var(--bad); } }
  }
  a.button {
    display: inline-flex; align-items: center;
    min-block-size: var(--tap); padding: 0.6rem 1.1rem; border-radius: 0.6rem;
    background: var(--accent); color: var(--accent-text); font-weight: 600; text-decoration: none;
  }
  .actions { display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; }

  .header-actions { display: flex; align-items: center; gap: 0.5rem; }
  button.icon-label {
    display: inline-flex; align-items: center; gap: 0.4rem; padding-inline: 0.8rem;
    & svg { inline-size: 1.15rem; block-size: 1.15rem; fill: currentColor; }
  }
  .qr-dialog {
    max-inline-size: min(22rem, calc(100vw - 2rem));
    padding: 1.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    text-align: center;
    /* Fade + scale in and out. allow-discrete keeps the dialog (and its top-layer
       backdrop) rendered while the close transition plays. */
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
    opacity: 0;
    scale: 0.94;
    translate: 0 0.5rem;
    transition:
      opacity 0.2s var(--ease), scale 0.2s var(--ease), translate 0.2s var(--ease),
      overlay 0.2s allow-discrete, display 0.2s allow-discrete;
    &::backdrop {
      background: rgb(0 0 0 / 0.5);
      opacity: 0;
      transition: opacity 0.2s ease, overlay 0.2s allow-discrete, display 0.2s allow-discrete;
    }
    &[open] {
      opacity: 1; scale: 1; translate: 0 0;
      &::backdrop { opacity: 1; }
    }
    @starting-style {
      &[open] { opacity: 0; scale: 0.94; translate: 0 0.5rem; }
      &[open]::backdrop { opacity: 0; }
    }
    & h2 { margin-block: 0 0.75rem; font-size: 1.1rem; }
    /* QR stays dark-on-white in dark mode so scanners read it. */
    & img { display: block; inline-size: 100%; max-inline-size: 15rem; block-size: auto; margin-inline: auto; border-radius: 0.5rem; background: #fff; }
    & .qr-url { overflow-wrap: anywhere; }
    & form { margin-block-start: 0.75rem; }
  }
  label.check { display: flex; align-items: center; gap: 0.5rem; min-block-size: var(--tap); }

  .rate-update {
    display: grid; gap: 0.75rem; margin-block-end: 1rem;
    & .update { display: grid; gap: 0.5rem; justify-items: start; }
    & .actions .hint { margin: 0; }
  }

  /* Rate-update toast: top on phones (clear of the tab bar and price peek), bottom-end on wide screens. */
  .toast {
    position: fixed;
    inset-inline: 0.75rem;
    inset-block-start: calc(env(safe-area-inset-top) + 0.75rem);
    z-index: 20;
    display: grid; gap: 0.6rem;
    padding: 0.9rem 1rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
    animation: toast-in 0.25s ease-out;
    & p { margin: 0; }
    & .actions { gap: 0.6rem; }
    @media (min-width: 52rem) {
      inset-block-start: auto; inset-inline-start: auto;
      inset-block-end: 1.5rem; inset-inline-end: 1.5rem;
      max-inline-size: 26rem;
    }
  }
  @keyframes toast-in { from { opacity: 0; translate: 0 -0.5rem; } }

  .notice {
    padding: 0.7rem 0.9rem;
    border-radius: 0.6rem;
    background: color-mix(in oklab, var(--accent) 9%, transparent);
    font-size: 0.9rem;
    &.warn { background: color-mix(in oklab, var(--warn) 14%, transparent); }
  }
  .status {
    font-size: 0.9rem;
    &.ok { color: var(--good); }
    &.warn { color: var(--warn); }
  }
  .offline-banner {
    font-size: 0.9rem;
    max-inline-size: 72rem; margin: 0 auto 1rem;
    padding: 0.6rem 0.9rem; border-radius: 0.6rem;
    background: color-mix(in oklab, var(--warn) 18%, var(--surface));
  }

  /* ---------- results ---------- */
  .results {
    scroll-margin-block-start: 1rem;
    @media (min-width: 52rem) { position: sticky; top: 1rem; }
  }

  /* Mobile: the answer stays visible above the tab bar while you type. */
  .peek {
    position: fixed;
    inset-inline: 0.75rem;
    inset-block-end: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 0.5rem);
    z-index: 9;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    column-gap: 0.6rem;
    padding: 0.6rem 1rem;
    border-radius: var(--radius);
    background: var(--accent);
    color: var(--accent-text);
    text-decoration: none;
    box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
    & strong { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
    transition: translate 0.2s ease, opacity 0.2s;
    [data-results-visible] & { translate: 0 150%; opacity: 0; pointer-events: none; }
    & .peek-label { font-size: 0.8rem; font-weight: 600; opacity: 0.85; }
    & .peek-meta { font-size: 0.8rem; opacity: 0.85; margin-inline-start: auto; }
    & .loss { color: inherit; text-decoration: underline wavy; }
    @media (min-width: 52rem) { display: none; }
  }
  .headline {
    & .eyebrow { color: var(--muted); font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
    & .price { font-size: var(--step-3); font-weight: 800; line-height: 1.05; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
    & .sub { color: var(--muted); }
  }
  .loss { color: var(--bad); }
  .pad {
    padding: 0.8rem 1rem;
    border-inline-start: 4px solid var(--tone-ship);
    background: var(--surface-2);
    border-radius: 0.5rem;
    display: grid; gap: 0.25rem;
  }
  .empty { color: var(--muted); padding-block: 2rem; text-align: center; }

  .breakdown {
    display: grid;
    font-variant-numeric: tabular-nums;
    & > div {
      display: flex; justify-content: space-between; gap: 1rem;
      padding-block: 0.4rem;
      border-block-end: 1px dashed var(--border);
    }
    & dd { white-space: nowrap; }
    & .muted { color: var(--muted); font-size: 0.9rem; }
    & .total { font-weight: 700; border-block-end-style: solid; }
  }

  .services {
    display: grid; gap: 0.5rem;
    & .service {
      display: grid;
      grid-template-columns: 1fr auto;
      grid-template-areas: "name cost" "meta meta" "badges badges";
      gap: 0.15rem 1rem;
      padding: 0.7rem 0.9rem;
      border: 1px solid var(--border);
      border-radius: 0.6rem;
      &.best { border-color: var(--good); }
    }
    & .name { grid-area: name; font-weight: 600; }
    & .meta { grid-area: meta; color: var(--muted); font-size: 0.875rem; }
    & .cost { grid-area: cost; font-weight: 700; font-variant-numeric: tabular-nums; }
    & .badges { grid-area: badges; display: flex; gap: 0.4rem; &:not(:has(*)) { display: none; } }
  }
  .badge {
    font-size: 0.75rem; font-weight: 700; padding: 0.1rem 0.5rem; border-radius: 999px; text-decoration: none;
    &.good { background: color-mix(in oklab, var(--good) 15%, transparent); color: var(--good); }
    &.warn { background: color-mix(in oklab, var(--warn) 15%, transparent); color: var(--warn); }
  }

  /* Wider result cards: breakdown in two columns, services in one row. */
  @container (min-width: 34rem) {
    .breakdown { grid-template-columns: 1fr 1fr; column-gap: 2rem; }
    .services .service {
      grid-template-columns: 1fr auto auto auto;
      grid-template-areas: "name meta badges cost";
      align-items: center;
    }
    .services .badges:not(:has(*)) { display: flex; }
  }

  /* ---------- editable tables ---------- */
  .wide { overflow: hidden; }
  .table-scroll { overflow: auto; overscroll-behavior: contain; max-block-size: 65dvh; border: 1px solid var(--border); border-radius: 0.6rem; }
  table { border-collapse: collapse; inline-size: 100%; font-variant-numeric: tabular-nums; }
  th, td { padding: 0.25rem; text-align: start; border-block-end: 1px solid var(--border); }
  thead th {
    position: sticky; top: 0; z-index: 1;
    background: var(--surface-2); padding: 0.5rem; font-size: 0.85rem; white-space: nowrap;
  }
  tbody th { white-space: nowrap; padding-inline: 0.75rem; font-weight: 600; font-size: 0.85rem; }
  .rates {
    & input { min-inline-size: 5rem; padding: 0.35rem 0.5rem; }
    & th.worst { background: color-mix(in oklab, var(--accent) 22%, var(--surface-2)); }
    & td.edited input { border-color: var(--good); }
  }
  .categories input[type="text"] { min-inline-size: 14rem; }
  .categories input[type="number"] { min-inline-size: 6rem; }
  .center { text-align: center; }
  .saved { animation: flash 1.2s ease-out; }
  @keyframes flash { from { background: color-mix(in oklab, var(--good) 30%, transparent); } }

  .tabs {
    display: flex; gap: 0.25rem; border-block-end: 1px solid var(--border);
    overflow-x: auto;
    & a {
      flex: none;
      padding: 0.6rem 0.9rem; text-decoration: none; color: var(--muted);
      border-block-end: 2px solid transparent; margin-block-end: -1px;
      &[aria-current="page"] { color: var(--text); border-color: var(--accent); font-weight: 600; }
    }
  }
}

@layer utilities {
  .visually-hidden {
    position: absolute; inline-size: 1px; block-size: 1px; overflow: hidden;
    clip-path: inset(50%); white-space: nowrap;
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after, ::backdrop { animation: none !important; transition: none !important; }
  }
}
