/* ════════════════════════════════════════════════════════════════════════
   Responsive layout — every breakpoint lives in this file (CLAUDE.md "Responsive layout").

   Breakpoints
     narrow  < 600px wide     single column, bottom tab bar          @container app (max-width: 599.98px)
     wide    ≥ 600px wide     side rail, multi-column screens        @container app (min-width: 600px)
     short   ≤ 500px tall     compact bars; chart sized from height  @media (max-height: 500px)
   Inside a screen, content-width breakpoints use the `view` / `coin` containers:
     view ≥ 640px   Home side-by-side sections, market/watchlist extra columns
     view < 280px   Market drops the 7-day sparkline (320px phones at the larger text sizes)
     view < 260px   Watchlist drops the price-source column and table logos (320px at the largest text size)
     coin ≥ 760px   coin page two columns (chart left, details right)

   Why container queries for width: the font-scale setting zooms #app (zoom 1.12 / 1.24). A container
   query measures #app in its own (zoomed) CSS pixels, so a 600px-wide layout switches on when 600 CSS px
   are really available at the chosen text size; a media query would measure the unzoomed viewport.
   Height uses a media query: "short" is about the physical screen (landscape phones, Fold cover).

   Why every viewport unit is divided by --zoom: Chrome scales dvh/vw inside a zoomed element
   (50dvh renders as 62dvh at zoom 1.24), so `calc(100dvh / var(--zoom))` is one real screen height.
   ════════════════════════════════════════════════════════════════════════ */

:root {
  --zoom: 1;
  --gutter: 16px;
  --rail-w: 80px;
  --content-max: 1200px;
  --settings-max: 720px;
  --screen-h: calc(100dvh / var(--zoom));
  --safe-t: calc(max(env(safe-area-inset-top, 0px), var(--native-inset-t, 0px)) / var(--zoom));
  --safe-r: calc(max(env(safe-area-inset-right, 0px), var(--native-inset-r, 0px)) / var(--zoom));
  --safe-b: calc(max(env(safe-area-inset-bottom, 0px), var(--native-inset-b, 0px)) / var(--zoom));
  --safe-l: calc(max(env(safe-area-inset-left, 0px), var(--native-inset-l, 0px)) / var(--zoom));
  --pad-l: max(var(--gutter), var(--safe-l));
  --pad-r: max(var(--gutter), var(--safe-r));
  --topbar-h: 48px;
  --ticker-h: 30px;
}
@media (max-height: 500px) { :root { --topbar-h: 40px; --ticker-h: 26px; } }

/* Font scale — component sizes are px (incl. inline styles), so zoom scales everything proportionally,
   like iOS Dynamic Type. Fixed layers sit outside #app and get the same zoom. */
[data-font-scale="1.12"] { --zoom: 1.12; }
[data-font-scale="1.24"] { --zoom: 1.24; }
[data-font-scale="1.12"] :is(#app, .coin-overlay, .sheet, .sheet-backdrop, .native-banner, .compat-notice, .pull-indicator) { zoom: 1.12; }
[data-font-scale="1.24"] :is(#app, .coin-overlay, .sheet, .sheet-backdrop, .native-banner, .compat-notice, .pull-indicator) { zoom: 1.24; }

/* ── App frame (narrow default) ─────────────────────────────────────────── */

#app { container: app / inline-size; }

.shell {
  display: flex;
  flex-direction: column;
  min-height: var(--screen-h);
}

#top-bar { padding: var(--safe-t) var(--pad-r) 0 var(--pad-l); }
#tab-content {
  container: view / inline-size;
  padding: 0 var(--pad-r) 16px var(--pad-l);
}
.tab-bar { padding: 0 var(--safe-r) var(--safe-b) var(--safe-l); }
.tab-view { margin: 0 auto; max-width: var(--content-max); }
#view-settings { max-width: var(--settings-max); }

/* ── Wide: side rail ─────────────────────────────────────────────────────
   The rail spends the spare width and leaves every vertical pixel to content, which matters most in
   landscape (844×390, Fold cover 882×344) where height is the scarce dimension. One rule for all
   wide sizes keeps navigation in the same place from Fold-unfolded to desktop. */
@container app (min-width: 600px) {
  .shell {
    display: grid;
    grid-template-columns: calc(var(--rail-w) + var(--safe-l)) minmax(0, 1fr);
    grid-template-rows: auto 1fr;
  }
  #top-bar { grid-column: 2; grid-row: 1; padding-left: var(--gutter); }
  #tab-content { grid-column: 2; grid-row: 2; padding-left: 24px; padding-right: max(24px, var(--safe-r)); }
  .tab-bar {
    grid-column: 1; grid-row: 1 / span 2;
    position: sticky; top: 0; align-self: start;
    height: var(--screen-h);
    flex-direction: column; justify-content: flex-start; gap: 4px;
    padding: calc(var(--safe-t) + 8px) 0 var(--safe-b) var(--safe-l);
    border-top: none; border-right: 1px solid var(--line);
    overflow-y: auto;
  }
  .tab-btn { flex: none; min-height: 64px; padding: 8px 4px; }
  .tab-label { font-size: 12px; text-align: center; line-height: 1.2; }
}

/* ── Short (landscape phones, Fold cover landscape) ──────────────────── */
/* The market search sticks just below the (sticky) top bar instead of sliding under it. */
.market-search { top: calc(var(--topbar-h) + var(--ticker-h) + var(--safe-t) + 1px); }
[data-tab="home"] { --ticker-h: 0px; }
.top-bar-inner { height: var(--topbar-h); }

@media (max-height: 500px) {
  .app-title { font-size: 15px; }
}
@container app (max-width: 599.98px) {
  @media (max-height: 500px) {
    /* Bottom bar: icon + inline label, one compact row. */
    .tab-btn { flex-direction: row; gap: 6px; min-height: 44px; padding: 0 4px; }
    .tab-icon { width: 20px; height: 20px; }
    .tab-label { font-size: 12px; white-space: nowrap; }
  }
}
@container app (min-width: 600px) {
  @media (max-height: 500px) {
    .tab-btn { min-height: 48px; gap: 1px; padding: 4px; }
    .tab-icon { width: 20px; height: 20px; }
    .tab-label { font-size: 12px; }
  }
}

/* ── Touch targets ≥ 44px ────────────────────────────────────────────── */
.chip, .btn, .coin-tool, .coin-trade-btn, .section-link { min-height: 44px; }
.chip, .coin-tool { min-width: 44px; display: inline-flex; align-items: center; justify-content: center; }
/* The chart library's TradingView attribution link (required attribution) is 35×19; grow its hit box up and
   to the right from the same bottom-left anchor, so the logo stays where the library draws it. */
.coin-chart #tv-attr-logo { min-width: 44px; min-height: 44px; display: flex; align-items: flex-end; justify-content: flex-start; }
.chip { align-items: center; }
.coin-back, .coin-star { width: 44px; height: 44px; }
.coin-style-btn { min-width: 44px; min-height: 44px; }
.coin-trade-link { min-height: 44px; align-items: center; }
.home-search, .market-search input { min-height: 44px; }
@media (hover: hover) {
  .coin-row:hover, .coin-table .tr:not(.th):hover { background: var(--panel2); }
  .tab-btn:hover { color: var(--text); }
}

/* ── Home ────────────────────────────────────────────────────────────── */
.home-cards {
  display: flex; gap: 10px; padding: 12px 0;
  overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain;
  scroll-snap-type: x proximity;
}
.home-cards > .card { min-width: 140px; flex-shrink: 0; scroll-snap-align: start; }
.home-cols > section { margin-top: 20px; min-width: 0; }
@container app (min-width: 600px) {
  .home-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); overflow: visible; }
  .home-cards > .card { min-width: 0; }
}
@container view (min-width: 640px) {
  .home-cols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 24px; align-items: start; }
}

/* ── Coin tables (Market, Watchlist) ────────────────────────────────────
   One grid for the whole list; rows are subgrids, so the header and every row share the same column
   tracks and a long price widens its column for all rows instead of wrapping. */
.coin-table {
  display: grid;
  grid-template-columns: minmax(0, 1fr) max-content max-content 60px;
  column-gap: 10px;
}
.coin-table .tr {
  display: grid; grid-column: 1 / -1; grid-template-columns: subgrid;
  align-items: center; min-height: 52px; padding: 6px 4px;
  border-bottom: 1px solid var(--line); cursor: pointer;
}
.coin-table .tr.th { min-height: 32px; cursor: default; font-size: 11px; font-weight: 600; color: var(--muted); }
.coin-table .tr:last-child { border-bottom: none; }
.coin-table .td { min-width: 0; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; font-size: 13px; }
.coin-table .td.name { display: flex; align-items: center; gap: 8px; text-align: left; overflow: hidden; }
.coin-table .td.spark svg { margin-left: auto; }
.coin-table .td.chg { font-weight: 600; font-size: 12px; }
.coin-table .wide-col { display: none; }
@container view (min-width: 640px) {
  .coin-table { grid-template-columns: minmax(160px, 1fr) max-content max-content max-content max-content max-content 72px; column-gap: 18px; }
  .coin-table .wide-col { display: block; }
}
@container view (max-width: 279.98px) {
  /* Only 320px phones at the larger text sizes; 360px phones (328px of content) keep the sparkline. */
  .coin-table { grid-template-columns: minmax(0, 1fr) max-content max-content; }
  .coin-table .spark { display: none; }
}
.card > .coin-table .tr { padding-left: 14px; padding-right: 14px; }

/* ── Coin page (overlay) ─────────────────────────────────────────────── */
.coin-overlay { container: coin / inline-size; overscroll-behavior: contain; }
.coin-overlay.hidden { visibility: hidden; transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), visibility 0s 0.3s; }
.coin-page {
  max-width: var(--content-max); margin: 0 auto;
  padding: 0 var(--pad-r) calc(32px + var(--safe-b)) var(--pad-l);
}
.coin-bar { padding-top: calc(8px + var(--safe-t)); }
.coin-chart { height: clamp(210px, calc(38dvh / var(--zoom)), 320px); }

/* Stat cards: three in a row when they fit (iOS), otherwise two + one; sized by the column they sit in
   (the whole page on phones, the right column on wide screens). Captions wrap rather than clip. */
.coin-body { container: coinbody / inline-size; }
@container coinbody (max-width: 419.98px) {
  .coin-stats { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
}
.coin-stat-label, .coin-stat-foot { white-space: normal; overflow: visible; }

/* Very narrow (320px phones at the largest text size, ~226px of content): icon-only trade button, no logo. */
@container coin (max-width: 359.98px) {
  .coin-trade-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .coin-trade-btn { min-width: 44px; }
}
@container coin (max-width: 299.98px) {
  .coin-bar > .coin-logo { display: none; }
}
.coin-ex-row { grid-template-columns: minmax(0, 1fr) auto auto; }
.coin-ex-row > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.coin-ex-row > span { white-space: nowrap; }

/* Wide: chart side (price, controls, chart, legend) left; details right. The last left row takes the
   slack, so a tall right column never stretches the price or chart rows. */
@container coin (min-width: 760px) {
  .coin-page {
    display: grid;
    grid-template-columns: minmax(0, 1.4fr) minmax(300px, 1fr);
    grid-template-rows: auto auto auto auto 1fr;
    column-gap: 28px;
    align-items: start;
  }
  .coin-bar { grid-column: 1 / -1; grid-row: 1; }
  .coin-price-block { grid-column: 1; grid-row: 2; }
  .coin-controls { grid-column: 1; grid-row: 3; }
  .coin-chart { grid-column: 1; grid-row: 4; height: clamp(260px, calc(55dvh / var(--zoom)), 520px); }
  .coin-chart-meta { grid-column: 1; grid-row: 5; }
  .coin-body { grid-column: 2; grid-row: 2 / span 4; margin-top: 12px; }
}

/* Short: the bar, price and range buttons stay on screen; the chart takes the height that is left. */
@media (max-height: 500px) {
  .coin-bar { padding-top: calc(4px + var(--safe-t)); padding-bottom: 4px; }
  .coin-price-block { display: flex; align-items: baseline; flex-wrap: wrap; column-gap: 10px; padding: 6px 0; }
  .coin-price-big { font-size: 20px; }
  .coin-controls { gap: 6px; margin-bottom: 6px; }
  .coin-page .coin-chart { height: max(140px, calc(var(--screen-h) - 190px)); }
}

/* ── Bottom sheet ────────────────────────────────────────────────────── */
.sheet {
  padding-left: max(16px, var(--safe-l)); padding-right: max(16px, var(--safe-r));
  max-height: calc(var(--screen-h) * 0.85);
  overscroll-behavior: contain;
}
@media (max-height: 500px) {
  .sheet { max-height: calc(var(--screen-h) - var(--safe-t) - 8px); padding-top: 12px; }
  .sheet-grabber { margin-bottom: 8px; }
}
/* Wide: a centered, width-limited dialog instead of a full-width sheet. */
@media (min-width: 600px) {
  .sheet {
    left: 50%; right: auto; bottom: auto; top: 50%;
    width: min(520px, calc(100vw / var(--zoom) - 48px));
    max-height: min(calc(var(--screen-h) - 48px), 720px);
    border-radius: 22px;
    padding-bottom: 16px;
    transform: translate(-50%, calc(-50% + 24px)); opacity: 0; pointer-events: none;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s;
  }
  .sheet.open { transform: translate(-50%, -50%); opacity: 1; pointer-events: auto; }
  .sheet-grabber { display: none; }
}
@media (min-width: 600px) and (max-height: 500px) {
  .sheet { max-height: calc(var(--screen-h) - var(--safe-t) - var(--safe-b) - 16px); }
}

/* ── Sheets: tall variant and on-screen keyboard (sheet.js sets --kb / --vv-h from visualViewport) ── */
.sheet--tall { height: var(--screen-h); }   /* capped by max-height: search on top, results fill the rest */
.sheet.kb-open { bottom: var(--kb, 0px); max-height: calc(var(--vv-h, var(--screen-h)) - var(--safe-t) - 8px); }
@media (min-width: 600px) {
  .sheet.kb-open { top: calc(var(--vv-h, var(--screen-h)) / 2); bottom: auto; max-height: calc(var(--vv-h, var(--screen-h)) - 16px); }
}
/* Keyboard up in a short viewport (landscape): drop the title and grabber and put close next to the search
   field, so the field and the first results fit in the little height that is left. */
@media (max-height: 500px) {
  .sheet.kb-open { padding-top: 6px; }
  .sheet.kb-open .sheet-grabber, .sheet.kb-open .sheet-title { display: none; }
  .sheet.kb-open .sheet-search { flex: 1 1 0; order: -1; }
  .sheet.kb-open .sheet-head { padding-bottom: 4px; }
}

/* ── Watchlist table (coinTable variant "watch") ─────────────────────────
   iOS columns: coin · my exchanges (+ premium) · price source · 24h change; wide adds the shared columns. */
.coin-table.watch { grid-template-columns: minmax(0, 1fr) max-content max-content max-content; column-gap: 10px; }
.coin-table.watch .th { min-height: 44px; }
/* Header labels wrap to two lines (iOS lineLimit(2)) so they never set the column width. */
.coin-table.watch .th .td:not(.name) { white-space: normal; line-height: 1.25; word-break: keep-all; }
.coin-table.watch .th .stack > span, .th-help > span:first-child { max-width: 4.6em; text-align: right; }
.coin-table.watch .th .td.chg { max-width: 4.6em; justify-self: end; }
.coin-table.watch .td.actions { display: none; }
/* Narrow: exactly the 4 iOS columns (the sparkline belongs to the wide set). */
.coin-table.watch .td.spark { display: none; }
.coin-table.watch .td.myx, .coin-table.watch .td.psrc { font-size: 11.5px; }
.coin-table .stack { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.coin-table .th .stack .sub { font-size: 9px; font-weight: 400; }
.coin-table .myx-val.est { font-style: italic; color: var(--muted); }
.coin-table .prem { font-size: 9.5px; font-weight: 600; color: var(--muted); }
.coin-table .range-bar.mini { width: 48px; height: 3px; }
.coin-table .range-bar.mini .range-bar-dot { width: 5px; height: 5px; }
.th-help { display: inline-flex; align-items: center; justify-content: flex-end; gap: 2px; white-space: normal; text-align: right; }
.help-btn {
  position: relative; width: 44px; height: 44px; margin: -12px -12px -12px -8px; flex-shrink: 0;
  border: none; background: none; color: var(--muted); font-size: 10px; font-weight: 700; cursor: pointer;
}
.help-btn::after { content: ''; position: absolute; inset: 12px; border: 1px solid var(--line); border-radius: 50%; }

@container view (min-width: 640px) {
  .coin-table.watch { grid-template-columns: minmax(160px, 1fr) max-content max-content max-content max-content max-content max-content 72px; column-gap: 18px; }
  .coin-table.watch .td.spark { display: block; }
}
@container view (max-width: 259.98px) {
  /* 320px phones at the largest text size (~226px of content): keep coin, my exchanges and change. */
  .coin-table.watch { grid-template-columns: minmax(0, 1fr) max-content max-content; }
  .coin-table.watch .td.psrc { display: none; }
  /* Names need the room more than logos do at this width. */
  .coin-table .td.name .coin-logo { display: none; }
}

/* Edit mode (iOS "코인 편집"): name + ↑ ↓ ✕ only. */
.coin-table.watch.editing { grid-template-columns: minmax(0, 1fr) max-content; }
.coin-table.watch.editing .td:not(.name):not(.actions) { display: none; }
.coin-table.watch.editing .td.actions { display: flex; gap: 2px; }

/* Swipe-to-delete and drag-to-reorder (list-gestures.js). */
.coin-table.watch .tr:not(.th) {
  position: relative; overflow: hidden;
  touch-action: pan-y; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.coin-table.watch .tr.swipe-open { --swipe: -76px; }
.coin-table.watch .tr > .td { transform: translateX(var(--swipe, 0px)); transition: transform 0.2s ease-out; }
.coin-table.watch .tr.swiping > .td, .coin-table.watch .tr.swiping > .row-swipe-del { transition: none; }
.row-swipe-del {
  position: absolute; top: 0; right: 0; bottom: 0; width: 76px;
  transform: translateX(calc(76px + var(--swipe, 0px))); transition: transform 0.2s ease-out;
  border: none; background: var(--down); color: var(--on-down); font-size: 13px; font-weight: 700; cursor: pointer;
}
.coin-table.watch .tr.dragging {
  z-index: 2; transform: translateY(var(--drag-y, 0px));
  background: var(--panel2); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}
.coin-table.watch .td.name { position: relative; }
.row-hover-del {
  display: none; position: absolute; right: 0; top: 50%; transform: translateY(-50%);
  min-width: 44px; min-height: 44px; align-items: center; justify-content: center;
  border: none; border-radius: 8px; background: var(--panel2); color: var(--down); font-size: 14px; cursor: pointer;
}
@media (hover: hover) {
  .coin-table.watch .tr:hover .row-hover-del,
  .coin-table.watch .tr:focus-within .row-hover-del { display: inline-flex; }
}
#exchangeOpts { scroll-margin-top: calc(var(--topbar-h) + var(--safe-t) + 8px); }

/* ── Below the browser baseline (CLAUDE.md) ──────────────────────────────
   No container queries: the defaults above are the narrow phone layout, so nothing is needed.
   No subgrid (Safari < 16, Chrome < 117): rows fall back to flex lines instead of collapsing to one column.
   No dvh (Safari < 15.4): use vh. */
@supports not (grid-template-columns: subgrid) {
  .coin-table { display: block; }
  .coin-table .tr { display: flex; align-items: center; gap: 10px; }
  .coin-table .td { flex: 0 0 auto; }
  .coin-table .td.name { flex: 1 1 auto; min-width: 0; }
  .coin-table .td.spark { width: 60px; }
}
@supports not (height: 1dvh) {
  :root { --screen-h: calc(100vh / var(--zoom)); }
}

/* ── Portfolio ───────────────────────────────────────────────────────────
   Narrow: one column; "Overview | Transactions" tabs switch the section (iOS sectionTabs).
   Wide (view ≥ 680px — Fold unfolded and up, ~340px per column): the overview (assets / allocation) and the
   transaction history side by side, tabs hidden — the history is what people check holdings against. */
.pf-cols[data-section="holdings"] .pf-col-trades,
.pf-cols[data-section="trades"] .pf-col-overview { display: none; }
@container view (min-width: 680px) {
  .pf-section-tabs { display: none; }
  .pf-cols { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 28px; align-items: start; }
  .pf-cols[data-section] .pf-col { display: block; }
  .pf-wallet { max-width: 560px; }
}
/* Holdings table (coinTable variant "pf"): coin · amount · value (+ ✕ in edit mode). */
.coin-table.pf { grid-template-columns: minmax(0, 1fr) max-content max-content; }
.coin-table.pf .td.actions { display: none; }
.coin-table.pf.editing { grid-template-columns: minmax(0, 1fr) max-content max-content max-content; }
.coin-table.pf.editing .td.actions { display: flex; }
.coin-table.pf .td.pf-amt, .coin-table.pf .td.pf-val { font-size: 11.5px; }
@container view (max-width: 299.98px) {
  .pf-coin-cards { flex-direction: column; }
}
/* Sheets live outside #app (no size container): the trade sheet's price/date pair and the exchange tiles
   wrap intrinsically (components.css), so they need no breakpoint here. */

/* ── Paper trading and Strategy lab (Phase 06) ──────────────────────────────
   Narrow: one column. Wide (view ≥ 680px, as the holdings screen): two columns — paper: summary / chart / goal
   beside holdings / orders / trades; strategy: coin / rule / parameters beside the backtest / follow / watches. */
@container view (min-width: 680px) {
  .paper-cols, .strategy-cols { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 28px; align-items: start; }
  .paper-cols .paper-col:last-child > .paper-sec:first-child { margin-top: 0; }
  .paper-actions { max-width: 560px; }
}
@container view (max-width: 299.98px) {
  .paper-stats, .strategy-metrics-3 { grid-template-columns: minmax(0, 1fr); }
  .strategy-kinds { grid-template-columns: minmax(0, 1fr); }
}
/* Short screens (phone / Fold-cover landscape): smaller charts so the content keeps the height. */
@media (max-height: 500px) {
  .paper-chart { height: 96px; }
  .strategy-chart { height: 110px; }
  .ps-pct { font-size: 34px; }
}
/* On-screen keyboard: a field scrolled into view (sheet.js) stops above the sticky submit button. */
.sheet-body :is(input, textarea, select) { scroll-margin-bottom: 76px; scroll-margin-top: 8px; }
