:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --row-active: #e9f0ff;
  --text: #15171c;
  --muted: #7b8190;
  --line: #e4e6eb;
  --accent: #2f6bff;
  --accent-soft: rgba(47, 107, 255, 0.12);
  --op: #e0782c;
  --danger: #d6453d;
  --up: #1f9d57;
  --down: #d6453d;
  --key: #ffffff;
  --key-fn: #eef0f4;
  --key-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), 0 0 0 1px rgba(0, 0, 0, 0.05);
  --pad-bg: #f0f1f4;
  --sheet: #ffffff;
  --backdrop: rgba(10, 12, 18, 0.35);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a20;
    --row-active: #1c2638;
    --text: #eef0f4;
    --muted: #8a90a0;
    --line: #262a33;
    --accent: #5b8cff;
    --accent-soft: rgba(91, 140, 255, 0.16);
    --op: #f0924a;
    --danger: #ff6b61;
    --up: #3ccf7f;
    --down: #ff6b61;
    --key: #252932;
    --key-fn: #1c2027;
    --key-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
    --pad-bg: #13161b;
    --sheet: #171a20;
    --backdrop: rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.3 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

.muted { color: var(--muted); }

/* Top bar */
.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 8px;
  background: var(--bg);
}
.topbar h1 { margin: 0; font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.status { font-size: 13px; color: var(--muted); min-height: 17px; }
.status.warn { color: var(--op); }
.text-btn { color: var(--accent); font-size: 17px; padding: 6px 2px; }

/* List */
.scroller {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
}
.list { list-style: none; margin: 0; padding: 0; }

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 68px;
  padding: 0 8px 0 16px;
  background: var(--bg);
  transition: background-color 0.15s;
}
.row + .row::before {
  content: "";
  position: absolute;
  top: 0; left: 72px; right: 0;
  border-top: 1px solid var(--line);
}
.row.active { background: var(--row-active); }
.row.active::before, .row.active + .row::before { display: none; }
.row.dragging {
  z-index: 5;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  transition: none;
}
.row.shifting { transition: transform 0.15s; }

.flag {
  flex: none;
  width: 44px;
  height: 32px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-size: 34px;
  line-height: 1;
  overflow: hidden;
}
.flag.badge {
  width: 44px;
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-radius: 16px;
}

.code {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 64px;
  max-width: 108px;
  text-align: left;
}
.code b { font-size: 20px; font-weight: 600; letter-spacing: 0.01em; }
.code b::after {
  content: "";
  display: inline-block;
  margin-left: 6px;
  vertical-align: middle;
  border: 5px solid transparent;
  border-top: 6px solid currentColor;
  border-bottom: 0;
  opacity: 0.55;
}
.code small { font-size: 12px; color: var(--muted); max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.value {
  flex: 1;
  min-width: 0;
  text-align: right;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  height: 100%;
}
.value .num {
  font-size: 24px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  color: var(--muted);
}
.row.active .value .num { color: var(--text); }
.value .res { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row.active .num::after {
  content: "";
  display: inline-block;
  width: 2px;
  height: 1em;
  margin-left: 2px;
  vertical-align: -0.12em;
  background: var(--accent);
  animation: blink 1s steps(1) infinite;
}
.row.active .num.fresh::after { display: none; }
.row.active .num.fresh { background: var(--accent-soft); border-radius: 4px; padding: 0 3px; }
@keyframes blink { 50% { opacity: 0; } }

.icon-btn {
  flex: none;
  width: 40px;
  height: 44px;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.del-btn { color: var(--danger); width: 28px; margin-left: -4px; }
.drag-handle { touch-action: none; cursor: grab; }

.add-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  height: 64px;
  padding: 0 16px;
  color: var(--muted);
  font-size: 18px;
}
.add-icon {
  width: 44px;
  height: 32px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-size: 22px;
  color: var(--accent);
  background: var(--accent-soft);
}

/* Keypad */
.pad {
  flex: none;
  background: var(--pad-bg);
  border-radius: 18px 18px 0 0;
  padding: 0 10px calc(env(safe-area-inset-bottom) + 10px);
  box-shadow: 0 -1px 0 var(--line);
  transition: transform 0.25s ease;
}
.pad-handle {
  display: block;
  width: 100%;
  height: 22px;
}
.pad-handle span {
  display: block;
  width: 38px;
  height: 5px;
  margin: 8px auto 0;
  border-radius: 3px;
  background: var(--line);
}
.keys {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  max-height: 400px;
  overflow: hidden;
  transition: max-height 0.25s ease, opacity 0.2s;
}
.pad.collapsed .keys { max-height: 0; opacity: 0; }
.key {
  height: 60px;
  border-radius: 12px;
  background: var(--key);
  box-shadow: var(--key-shadow);
  font-size: 26px;
  font-weight: 400;
  display: grid;
  place-items: center;
  transition: transform 0.06s, filter 0.06s;
}
.key:active, .key.pressed { transform: scale(0.95); filter: brightness(0.92); }
.key.fn { background: var(--key-fn); }
.key.op { color: var(--op); font-size: 28px; }
.key.danger { color: var(--danger); }
.key.accent { color: var(--accent); }
.key.eq { background: var(--op); color: #fff; }
.key.spin svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Sheets */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: var(--backdrop); animation: fade 0.2s; }
.sheet-panel {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  max-height: calc(100% - env(safe-area-inset-top) - 20px);
  display: flex;
  flex-direction: column;
  background: var(--sheet);
  max-width: 560px;
  margin: 0 auto;
  border-radius: 18px 18px 0 0;
  padding: 12px 16px calc(env(safe-area-inset-bottom) + 16px);
  animation: up 0.25s ease;
}
.sheet-panel.tall { height: calc(100% - env(safe-area-inset-top) - 20px); }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }

.sheet-head { display: flex; gap: 12px; align-items: center; padding-bottom: 8px; }
.search {
  flex: 1;
  height: 40px;
  border: 0;
  border-radius: 10px;
  padding: 0 12px;
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--key-fn);
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
.pick-list { list-style: none; margin: 0 -16px; padding: 0; overflow-y: auto; flex: 1; }
.pick-list li.group {
  padding: 14px 16px 6px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.pick {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  text-align: left;
}
.pick:active { background: var(--key-fn); }
.pick .names { flex: 1; min-width: 0; }
.pick .names b { display: block; font-weight: 600; }
.pick .names small { display: block; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pick .check { color: var(--accent); font-size: 20px; width: 20px; }

/* Chart */
.chart-head { display: flex; justify-content: space-between; align-items: center; }
.pair {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 600;
  padding: 6px 10px;
  margin-left: -10px;
  border-radius: 10px;
}
.pair:active { background: var(--key-fn); }
.pair .flag { width: 30px; height: 22px; font-size: 24px; border-radius: 4px; }
.pair .flag.badge { font-size: 11px; border-radius: 11px; }
.pair .swap { color: var(--accent); }
.chart-value { padding: 10px 0 6px; }
.chart-rate { font-size: 32px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.chart-meta { display: flex; gap: 10px; font-size: 14px; min-height: 18px; }
.chg { font-weight: 600; font-variant-numeric: tabular-nums; }
.chg.up { color: var(--up); }
.chg.down { color: var(--down); }
.chart-box { position: relative; height: 250px; margin: 8px 0 0; touch-action: none; }
.chart-box svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart-box .msg { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 15px; }
.periods {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  margin-top: 12px;
  padding: 3px;
  border-radius: 10px;
  background: var(--key-fn);
}
.periods button { height: 32px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--muted); }
.periods button.on { background: var(--sheet); color: var(--text); box-shadow: var(--key-shadow); }

.grid-line { stroke: var(--line); stroke-width: 1; stroke-dasharray: 2 4; }
.axis-line { stroke: var(--line); stroke-width: 1; }
.axis-label { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.series { fill: none; stroke: var(--accent); stroke-width: 2.25; stroke-linejoin: round; stroke-linecap: round; }
.series.faded { opacity: 0; transition: opacity 0.15s; }
.series.draw { stroke-dasharray: 1; stroke-dashoffset: 1; animation: draw 0.7s ease-out forwards; }
.area { animation: fade 0.7s ease-out; }
@keyframes draw { to { stroke-dashoffset: 0; } }

.tick { stroke: var(--line); stroke-width: 1.5; stroke-linecap: round; transition: stroke 0.1s; }
.tick.major { stroke: var(--muted); opacity: 0.6; }
.tick.on { stroke: var(--accent); stroke-width: 2.5; opacity: 1; }
.x-labels, #extremes { transition: opacity 0.15s; }
.extreme circle { fill: var(--sheet); stroke: var(--muted); stroke-width: 1.5; }
.extreme text { fill: var(--muted); font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }

.chart-box.scrubbing .series.faded { opacity: 0.28; }
.chart-box.scrubbing .x-labels,
.chart-box.scrubbing #extremes { opacity: 0; }
.cross { stroke: var(--accent); stroke-width: 1; opacity: 0.5; }
.halo { fill: var(--accent); opacity: 0.16; }
.dot { fill: var(--accent); stroke: var(--sheet); stroke-width: 2.5; }
.date-pill {
  position: absolute;
  padding: 3px 8px;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
}

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 12px);
  transform: translateX(-50%);
  z-index: 40;
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  max-width: calc(100% - 32px);
}

@media (max-height: 700px) {
  .key { height: 50px; }
  .row { height: 60px; }
}
