:root {
  --bg: #f2f2f7; --card: #ffffff; --text: #111113; --muted: #6c6c70; --line: #e3e3e8; --fill: #e9e9ee;
  --red: #dc3330; --blue: #2166db; --accent: #0a7aff; --warn-bg: #fff4c2;
  --up: var(--red); --down: var(--blue);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000; --card: #1c1c1e; --text: #f2f2f7; --muted: #98989f; --line: #2c2c2e; --fill: #2c2c2e;
    --red: #ff5a52; --blue: #4b8dff; --accent: #4b9bff; --warn-bg: #4a3d00;
  }
}
body.blue-up { --up: var(--blue); --down: var(--red); }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; padding: 48px 16px; }
.pos { color: var(--up); }
.neg { color: var(--down); }
.zero { color: var(--muted); }

.topbar {
  position: sticky; top: 0; z-index: 5; display: grid; grid-template-columns: 44px 1fr 44px; align-items: center;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 { grid-column: 2; margin: 0; font-size: 17px; font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-button {
  width: 40px; height: 40px; border: 0; border-radius: 50%; background: var(--card); color: var(--text);
  font-size: 22px; line-height: 40px; text-align: center; text-decoration: none; cursor: pointer; padding: 0;
}
#back { grid-column: 1; font-size: 28px; line-height: 36px; }
#open-settings { grid-column: 3; }
[hidden] { display: none !important; }

main#view { max-width: 720px; margin: 0 auto; padding: 4px 12px 32px; display: grid; gap: 12px; }

.market-switch { display: grid; grid-template-columns: 1fr 1fr; background: var(--fill); border-radius: 12px; padding: 3px; }
.market-switch a {
  text-align: center; text-decoration: none; color: var(--muted); font-weight: 600; font-size: 15px;
  padding: 9px 4px; border-radius: 9px; letter-spacing: .02em;
}
.market-switch a[aria-current="page"] { background: var(--accent); color: #fff; box-shadow: 0 1px 3px #0003; }

.status { display: grid; gap: 3px; }
.status-top { display: flex; align-items: center; gap: 8px; }
.status-top .date { font-weight: 600; }
.badge { font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--fill); }
.badge.live { background: #34c75940; } .badge.after_hours { background: #ff950040; } .badge.pre_open { background: #ffcc0050; }
.status-top .refreshed { margin-left: auto; }

.banner { background: var(--warn-bg); border-radius: 10px; padding: 10px 12px; font-size: 13px; }

.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile { background: var(--card); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.tile .label { font-size: 12px; color: var(--muted); }
.tile .value { font-size: 21px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .foot { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.options { display: grid; gap: 8px; }
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--fill); border-radius: 9px; padding: 2px; }
.segmented button {
  border: 0; background: transparent; color: var(--text); font: inherit; font-size: 13px; padding: 7px 4px;
  border-radius: 7px; cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px #0002; }
.segmented button:disabled { opacity: .4; cursor: default; }
.options-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.options select {
  font: inherit; font-size: 13px; color: var(--accent); background: transparent; border: 0; padding: 7px 2px; max-width: 130px;
}

.card { background: var(--card); border-radius: 14px; padding: 12px; display: grid; gap: 8px; }
.sector-head { display: flex; align-items: baseline; gap: 8px; }
.rank { min-width: 24px; height: 24px; border-radius: 12px; background: var(--fill); font-size: 12px; font-weight: 700; text-align: center; line-height: 24px; align-self: center; }
.sector-head h2 { margin: 0; font-size: 17px; }
.metrics { display: flex; justify-content: space-between; gap: 8px; }
.metric { min-width: 0; }
.metric .label { font-size: 11px; color: var(--muted); }
.metric .value { font-size: 16px; font-weight: 600; white-space: nowrap; }
.bar { height: 4px; border-radius: 2px; overflow: hidden; display: flex; background: transparent; }
.bar .buy { background: var(--up); } .bar .sell { background: var(--down); }
.divider { height: 1px; background: var(--line); }

.rows { display: grid; }
a.row {
  display: grid; grid-template-columns: 20px minmax(0, 1fr) auto 56px; gap: 8px; align-items: center;
  padding: 6px 0; color: inherit; text-decoration: none; border-top: 1px solid var(--line);
}
.rows a.row:first-child { border-top: 0; }
.wide-only { display: none; }
.cards, .split { display: grid; gap: 12px; }
.row .idx { font-size: 11px; color: var(--muted); text-align: right; }
.row .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { font-size: 11px; white-space: nowrap; }
.row .right { text-align: right; }
.row .ratio { font-weight: 600; }
a.more { display: block; text-align: center; color: var(--accent); text-decoration: none; font-size: 13px; font-weight: 500; padding-top: 4px; }
.note { font-size: 11px; color: var(--muted); }

.price-line { display: flex; align-items: baseline; gap: 8px; }
.price-line .price { font-size: 24px; font-weight: 600; }
.price-line .sector { margin-left: auto; font-size: 13px; color: var(--muted); }
.card h3 { margin: 0; font-size: 13px; font-weight: 500; color: var(--muted); }
svg.chart { width: 100%; height: auto; display: block; }
svg.chart .grid { stroke: var(--line); stroke-width: 1; }
svg.chart .zero-line { stroke: var(--muted); stroke-width: 1; }
svg.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
svg.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; }
svg.chart .line.pos { stroke: var(--up); } svg.chart .line.neg { stroke: var(--down); } svg.chart .line.zero { stroke: var(--muted); }

dialog { border: 0; border-radius: 16px; background: var(--card); color: var(--text); width: min(92vw, 420px); padding: 0; }
dialog::backdrop { background: #0006; }
.settings { padding: 18px; display: grid; gap: 14px; }
.settings h2 { margin: 0; font-size: 18px; }
.settings fieldset { border: 1px solid var(--line); border-radius: 10px; display: grid; gap: 8px; padding: 10px 12px; margin: 0; }
.settings legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.settings-actions { display: flex; justify-content: space-between; gap: 8px; }
button.primary, button.danger, .login-card button {
  font: inherit; font-weight: 600; border: 0; border-radius: 10px; padding: 10px 16px; cursor: pointer;
  background: var(--accent); color: #fff;
}
button.danger { background: transparent; color: var(--red); }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--card); border-radius: 16px; padding: 24px; width: min(100%, 360px); }
.login-card h1 { margin: 0 0 16px; font-size: 20px; }
.login-card form { display: grid; gap: 10px; }
.login-card label { font-size: 13px; color: var(--muted); }
.login-card input {
  font: inherit; font-size: 16px; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text);
}
.login-card button:disabled { opacity: .5; }
.message { min-height: 1.3em; margin: 0; font-size: 13px; color: var(--red); }

/* ---------------------------------------------------------------- wider screens
   The layout follows the window width, not the browser name: a tablet or a narrow
   desktop window gets whichever arrangement fits. */
@media (hover: hover) {
  a.row:hover { background: color-mix(in srgb, var(--fill) 55%, transparent); }
  .segmented button:not(:disabled):not([aria-pressed="true"]):hover { background: color-mix(in srgb, var(--card) 55%, transparent); }
  .icon-button:hover, a.more:hover { filter: brightness(0.95); }
}

@media (min-width: 700px) {
  body { font-size: 15px; }
  .market-switch { max-width: 360px; }
  main#view { max-width: 1480px; padding: 8px 24px 40px; gap: 16px; }
  .topbar { padding-left: 20px; padding-right: 20px; }
  .tiles { grid-template-columns: repeat(4, 1fr); gap: 12px; }
  .options { grid-template-columns: 1fr 1fr; align-items: center; gap: 12px; }
  .options > .small { grid-column: 1 / -1; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 16px; align-items: start; }
  .card { padding: 16px; }
  a.row { padding: 7px 6px; border-radius: 6px; }
  .login-card { padding: 32px; }
}

@media (min-width: 1000px) {
  .options { grid-template-columns: minmax(280px, 420px) minmax(280px, 380px) 1fr; }
  .options > .small { grid-column: auto; text-align: right; }
  /* Sector page: a real table, with buy and sell as their own columns. */
  .wide-table .wide-only { display: block; }
  .wide-table .row-head { display: grid; }
  .wide-table .row { grid-template-columns: 28px minmax(0, 1fr) 110px 110px 130px 90px; gap: 16px; }
  .row-head { align-items: end; padding: 0 6px 6px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
  .wide-table a.row:first-of-type { border-top: 0; }
  /* Stock page: figures on the left, the day's chart on the right. */
  .split { grid-template-columns: minmax(340px, 2fr) 3fr; gap: 16px; align-items: start; }
}
