:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --key: #ffffff;
  --key-hover: #f1efea;
  --fn: #ebe9e3;
  --fn-hover: #e0ddd5;
  --text: #1b1c1f;
  --muted: #6b6d73;
  --border: #e2dfd8;
  --accent: #0f766e;
  --accent-hover: #0b5f58;
  --accent-soft: #d9efec;
  --accent-text: #ffffff;
  --danger: #b4372f;
  --up: #15803d;
  --down: #c2410c;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a; --surface: #1f2024; --surface-2: #26282d; --key: #2a2c31; --key-hover: #33363c;
    --fn: #222428; --fn-hover: #2c2f34; --text: #ececee; --muted: #9a9ca3; --border: #34363c;
    --accent: #2dd4bf; --accent-hover: #5eead4; --accent-soft: #123a37; --accent-text: #08201d;
    --danger: #f2877e; --up: #4ade80; --down: #fb7d5b; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a; --surface: #1f2024; --surface-2: #26282d; --key: #2a2c31; --key-hover: #33363c;
  --fn: #222428; --fn-hover: #2c2f34; --text: #ececee; --muted: #9a9ca3; --border: #34363c;
  --accent: #2dd4bf; --accent-hover: #5eead4; --accent-soft: #123a37; --accent-text: #08201d;
  --danger: #f2877e; --up: #4ade80; --down: #fb7d5b; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
code, kbd { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- header ---------- */
.top { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; margin-right: auto; }
.logo {
  width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-size: 17px;
}
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; overflow-x: auto; }
.tabs button {
  border: 0; background: transparent; padding: 7px 14px; border-radius: 9px; cursor: pointer;
  color: var(--muted); font-weight: 500; white-space: nowrap;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); width: 38px; height: 38px; border-radius: 10px;
  cursor: pointer; font-size: 17px; display: grid; place-items: center; flex: none;
}
.icon-btn:hover { background: var(--key-hover); }
.icon-btn.small { width: 30px; height: 30px; font-size: 14px; border-color: transparent; background: transparent; color: var(--muted); }
.link-btn { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 4px 0; font-weight: 500; }
.link-btn:hover { text-decoration: underline; }

@media (max-width: 640px) {
  .top { gap: 10px; }
  .tabs { order: 3; width: 100%; }
  .tabs button { flex: 1; padding: 7px 8px; }
}

/* ---------- layout ---------- */
.panel { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; flex: 1; }
@media (max-width: 860px) { .panel { grid-template-columns: minmax(0, 1fr); } }

.calc, .history, .graph-side, .convert, .prog {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}

/* ---------- display ---------- */
.display {
  background: var(--surface-2); border-radius: 12px; padding: 10px 14px 8px; margin-bottom: 12px;
}
.status { display: flex; gap: 6px; min-height: 20px; }
.badge {
  font-size: 11px; font-weight: 600; letter-spacing: .04em; padding: 1px 7px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}
.expr {
  width: 100%; border: 0; background: transparent; outline: none; text-align: right;
  font-family: var(--mono); font-size: clamp(22px, 5vw, 32px); padding: 6px 0 2px; color: var(--text);
}
.expr::placeholder { color: var(--muted); }
.result-row { display: flex; align-items: center; justify-content: flex-end; gap: 4px; min-height: 30px; }
.preview {
  font-family: var(--mono); font-size: 18px; color: var(--muted); text-align: right;
  overflow-x: auto; white-space: nowrap; max-width: calc(100% - 34px);
}
.preview.final { color: var(--accent); font-weight: 600; }
.preview.error { color: var(--danger); font-size: 14px; }

/* ---------- keys ---------- */
.toolbar { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-bottom: 10px; }
.keys { display: grid; grid-template-columns: 5fr 4fr; gap: 14px; }
.sci { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.num { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
@media (max-width: 640px) {
  .keys { grid-template-columns: 1fr; gap: 10px; }
  .sci .key { min-height: 38px; font-size: 14px; }
}

.key {
  border: 1px solid var(--border); background: var(--key); border-radius: 10px; min-height: 48px;
  cursor: pointer; font-size: 17px; user-select: none; touch-action: manipulation;
  transition: background .1s, transform .05s;
}
.key:hover { background: var(--key-hover); }
.key:active { transform: scale(.97); }
.key:focus-visible, .tabs button:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.key.fn, .key.tool { background: var(--fn); border-color: transparent; font-size: 15px; }
.key.fn:hover, .key.tool:hover { background: var(--fn-hover); }
.key.tool { min-height: 36px; font-size: 13px; font-weight: 600; }
.key.tool.on { background: var(--accent-soft); color: var(--accent); }
.key.op { color: var(--accent); font-weight: 600; font-size: 20px; }
.key.clear { color: var(--danger); font-weight: 600; }
.key.equals { background: var(--accent); color: var(--accent-text); border-color: transparent; font-size: 22px; font-weight: 600; }
.key.equals:hover { background: var(--accent-hover); }
.key.fn.alt { color: var(--accent); }

/* ---------- history ---------- */
.history h2 { font-size: 15px; margin: 0; }
.history-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.history-list { list-style: none; margin: 0; padding: 0; max-height: 360px; overflow-y: auto; }
.history-list li {
  padding: 8px 10px; border-radius: 9px; cursor: pointer; text-align: right; font-family: var(--mono);
}
.history-list li:hover { background: var(--surface-2); }
.history-list .h-expr { color: var(--muted); font-size: 13px; word-break: break-all; }
.history-list .h-res { font-size: 17px; word-break: break-all; }
.help { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; font-size: 13px; }
.help summary { cursor: pointer; font-weight: 500; }
.help ul { padding-left: 18px; margin: 8px 0 0; }
.help li { margin-bottom: 6px; }

/* ---------- graph ---------- */
#panel-graph { grid-template-columns: 300px minmax(0, 1fr); }
@media (max-width: 860px) { #panel-graph { grid-template-columns: minmax(0, 1fr); } #panel-graph .graph-wrap { order: -1; } }
.fn-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 6px; }
.fn-row { display: flex; align-items: center; gap: 6px; }
.fn-row .swatch { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.fn-row .label { font-family: var(--mono); font-size: 13px; color: var(--muted); flex: none; }
.fn-row input {
  flex: 1; min-width: 0; border: 1px solid var(--border); background: var(--bg); border-radius: 9px;
  padding: 8px 10px; font-family: var(--mono); outline: none;
}
.fn-row input:focus { border-color: var(--accent); }
.fn-row input.bad { border-color: var(--danger); }
.fn-row .rm { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 18px; padding: 0 4px; }
.graph-ctrls { display: flex; gap: 6px; margin: 12px 0; }
.graph-ctrls .key { flex: 1; }
.graph-wrap {
  position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; aspect-ratio: 4 / 3; max-width: 100%;
}
#graph { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#graph.dragging { cursor: grabbing; }
.graph-tip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px 8px; font-family: var(--mono); font-size: 12px; box-shadow: var(--shadow);
  white-space: nowrap;
}

/* ---------- convert / programmer ---------- */
#panel-convert, #panel-prog { grid-template-columns: minmax(0, 720px); justify-content: center; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 500; }
.field input, .field select {
  border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 9px 11px; outline: none;
  min-width: 0; width: 100%;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.conv-row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.conv-row .field { flex: 1 1 140px; }
.conv-row .grow { flex: 2 1 160px; }
.swap { margin-bottom: 12px; }
.conv-result {
  background: var(--accent-soft); color: var(--accent); border-radius: 12px; padding: 14px 16px;
  font-family: var(--mono); font-size: 20px; font-weight: 600; word-break: break-all; margin-bottom: 8px;
}
.convert h3 { font-size: 15px; margin: 18px 0 8px; }
.table-wrap { overflow-x: auto; }
.conv-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.conv-table td { padding: 7px 8px; border-bottom: 1px solid var(--border); }
.conv-table td:first-child { color: var(--muted); }
.conv-table td:last-child { text-align: right; font-family: var(--mono); word-break: break-all; }
.conv-table tr.sel td { color: var(--accent); font-weight: 600; }
.mono { font-family: var(--mono); }
.err { color: var(--danger); font-size: 14px; }

.foot { text-align: center; color: var(--muted); font-size: 12px; padding-top: 4px; }

/* ---------- markets ---------- */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.live-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--up); margin-left: 4px; vertical-align: middle; animation: pulse 2s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }
#panel-markets { grid-template-columns: minmax(0, 1fr) 360px; }
@media (max-width: 860px) { #panel-markets { grid-template-columns: minmax(0, 1fr); } }
.mk, .mk-detail {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.mk-detail { position: sticky; top: calc(env(safe-area-inset-top, 0px) + 12px); }
@media (max-width: 860px) { .mk-detail { position: static; order: -1; } }
.mk-top { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.mk-search { position: relative; flex: 1 1 260px; }
.mk-search input {
  width: 100%; border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 9px 12px; outline: none;
}
.mk-search input:focus { border-color: var(--accent); }
.mk-results {
  position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 4px); list-style: none; margin: 0; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow);
  max-height: 340px; overflow-y: auto;
}
.mk-results li { display: flex; gap: 10px; align-items: baseline; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.mk-results li:hover, .mk-results li:focus { background: var(--surface-2); outline: none; }
.mk-results .r-sym { font-family: var(--mono); font-weight: 600; flex: none; }
.mk-results .r-name { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mk-status.err { color: var(--danger); }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; margin-bottom: 4px; scrollbar-width: thin; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; cursor: pointer;
  white-space: nowrap; font-size: 13px; font-weight: 500; color: var(--muted);
}
.chip:hover { background: var(--key-hover); }
.chip[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.mk-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.mk-table th { text-align: left; font-size: 12px; font-weight: 500; color: var(--muted); padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.mk-table th.num { text-align: right; }
.mk-table td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.mk-table tbody tr { cursor: pointer; }
.mk-table tbody tr:hover { background: var(--surface-2); }
.mk-table tbody tr.sel { background: var(--accent-soft); }
.mk-table tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.mk-table .num { text-align: right; font-family: var(--mono); white-space: nowrap; font-variant-numeric: tabular-nums; }
.mk-table .px { font-weight: 600; border-radius: 6px; }
.c-star { width: 30px; padding-right: 0 !important; }
.star { border: 0; background: none; cursor: pointer; font-size: 17px; color: var(--muted); padding: 2px; line-height: 1; }
.star.on { color: #e0a500; }
.c-name { max-width: 240px; }
.c-name .n1 { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-name .n2 { font-size: 12px; color: var(--muted); font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }
.pill { display: inline-block; min-width: 72px; text-align: right; padding: 2px 8px; border-radius: 6px; font-weight: 600; }
.pill.up { background: color-mix(in srgb, var(--up) 14%, transparent); }
.pill.down { background: color-mix(in srgb, var(--down) 14%, transparent); }
.rng { min-width: 130px; }
.bar { position: relative; height: 4px; border-radius: 2px; background: var(--surface-2); margin: 4px 0; }
.bar .mark { position: absolute; top: -3px; width: 4px; height: 10px; border-radius: 2px; background: var(--text); transform: translateX(-50%); }
.lohi { font-size: 11px; color: var(--muted); font-family: var(--mono); white-space: nowrap; }
.tm { font-size: 12px; color: var(--muted); white-space: nowrap; }
.tm .tag { margin-left: 6px; }
.tag { display: inline-block; font-size: 10px; font-weight: 600; letter-spacing: .03em; padding: 1px 6px; border-radius: 999px; text-transform: uppercase; }
.tag.delayed { background: var(--surface-2); color: var(--muted); }
.tag.live { background: color-mix(in srgb, var(--up) 16%, transparent); color: var(--up); }
.tag.ext { background: var(--accent-soft); color: var(--accent); }
@keyframes flashUp { from { background: color-mix(in srgb, var(--up) 35%, transparent); } to { background: transparent; } }
@keyframes flashDown { from { background: color-mix(in srgb, var(--down) 35%, transparent); } to { background: transparent; } }
.flash-up { animation: flashUp .9s ease-out; }
.flash-down { animation: flashDown .9s ease-out; }
@media (max-width: 1000px) { .hide-md { display: none; } }
@media (max-width: 560px) { .hide-sm { display: none; } .mk-table td, .mk-table th { padding: 7px 5px; } .pill { min-width: 62px; } .c-name { max-width: 150px; } }
.disclaimer { margin: 14px 0 0; }

.d-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.d-title { min-width: 0; }
.d-name { font-weight: 600; font-size: 16px; }
.d-sym { font-family: var(--mono); }
#dStar.on { color: #e0a500; }
.d-price { margin-top: 8px; }
.d-last { font-family: var(--mono); font-size: 30px; font-weight: 600; border-radius: 6px; font-variant-numeric: tabular-nums; }
.d-chg { font-family: var(--mono); font-weight: 600; margin-bottom: 2px; }
.d-ranges { display: flex; gap: 4px; margin: 12px 0 6px; background: var(--surface-2); padding: 3px; border-radius: 9px; }
.d-ranges button { flex: 1; border: 0; background: none; padding: 5px 0; border-radius: 7px; cursor: pointer; font-size: 12px; font-weight: 600; color: var(--muted); }
.d-ranges button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.d-chart { position: relative; height: 230px; }
#dCanvas { width: 100%; height: 100%; display: block; touch-action: pan-y; }
.d-chart-msg { position: absolute; inset: 0; display: grid; place-items: center; }
.d-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; margin: 12px 0 0; }
.d-stats .stat { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.d-stats dt { color: var(--muted); }
.d-stats dd { margin: 0; font-family: var(--mono); text-align: right; }

.gold { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 14px; }
.gold h3 { font-size: 15px; margin: 0 0 10px; }
.gold-ctrls { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0 10px; }
.gold-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.gold-table th:not(:first-child) { text-align: right; }
.gold-table td:not(:first-child) { text-align: right; font-family: var(--mono); }
.gold-table td { color: var(--text) !important; font-weight: 400 !important; }
.gold-table td.muted { color: var(--muted) !important; text-align: left !important; font-family: inherit !important; }
