:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --ink: #15171a;
  --muted: #696e74;
  --line: #e3e0d8;
  --accent: #0b6e4f;
  --accent-ink: #ffffff;
  --accent-soft: #e2f0e9;
  --key: #ffffff;
  --key-fn: #e9e6df;
  --key-op: #dbede4;
  --warn: #a4560f;
  --danger: #b3261e;
  --shadow: 0 1px 0 rgba(0, 0, 0, .03), 0 1px 3px rgba(0, 0, 0, .07);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1011;
    --surface: #191c1e;
    --ink: #f0f0ec;
    --muted: #9aa0a5;
    --line: #2a2e31;
    --accent: #3fc596;
    --accent-ink: #05231a;
    --accent-soft: #15302a;
    --key: #202427;
    --key-fn: #2b3034;
    --key-op: #173a2e;
    --warn: #e6a65e;
    --danger: #ff8a80;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: var(--accent); }

.app {
  height: 100vh;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 12px) max(16px, env(safe-area-inset-left));
}

/* ---------- Header ---------- */
.top { display: flex; align-items: center; gap: 8px; padding: 12px 0 10px; }
.brand { flex: 1; min-width: 0; }
.brand h1 { margin: 0; font-size: 22px; letter-spacing: -.01em; line-height: 1.15; }
.status { font-size: 13px; color: var(--muted); text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.status.warn { color: var(--warn); font-weight: 600; }
.pill {
  height: 38px; padding: 0 14px; border-radius: 19px;
  background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--line);
  font-size: 15px; font-weight: 600; flex: none;
}
.pill.round { width: 38px; padding: 0; display: grid; place-items: center; }
.pill:active { background: var(--key-fn); }

/* ---------- Currency rows ---------- */
.rows {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 8px;
  padding: 2px 0 12px;
}
.row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 10px;
  row-gap: 2px;
  padding: 10px 14px 10px 10px;
  min-height: 78px;
  background: var(--surface);
  border: 2px solid transparent;
  border-radius: 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  flex: none;
}
.row.active { border-color: var(--accent); }
.cur {
  grid-column: 1; grid-row: 1;
  display: flex; align-items: center; gap: 7px;
  padding: 4px 8px 4px 4px; border-radius: 10px;
}
.cur:active { background: var(--bg); }
.flag { font-size: 24px; line-height: 1; }
.code { font-weight: 700; font-size: 18px; letter-spacing: .02em; }
.caret { color: var(--muted); font-size: 11px; }
.meta {
  grid-column: 1 / -1; grid-row: 2;
  display: flex; align-items: center; gap: 6px;
  padding-left: 4px; min-width: 0; min-height: 26px;
  font-size: 13px; color: var(--muted);
}
.name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.home {
  font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--accent); background: var(--accent-soft);
  padding: 2px 6px; border-radius: 6px; flex: none;
}
.fee {
  min-height: 26px; padding: 0 9px; border-radius: 13px; flex: none;
  font-size: 12px; font-weight: 600; color: var(--muted);
  border: 1px solid var(--line); margin-left: auto;
}
.fee.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.amt {
  grid-column: 2; grid-row: 1;
  display: flex; flex-direction: column; align-items: flex-end; justify-content: center;
  min-width: 0; text-align: right;
}
.expr {
  display: flex; justify-content: flex-end; overflow: hidden; max-width: 100%;
  font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.val {
  --len: 1;
  font-size: calc(34px * min(1, calc(10 / var(--len))));
  font-weight: 600; letter-spacing: -.02em; line-height: 1.15;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.val.empty { color: var(--muted); opacity: .45; }
.row.active .val::after {
  content: ""; display: inline-block; width: 2px; height: .9em; margin-left: 2px;
  background: var(--accent); vertical-align: -.1em;
  animation: blink 1.1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.add {
  flex: none; min-height: 50px; border-radius: 16px;
  border: 2px dashed var(--line); color: var(--muted); font-weight: 600;
}
.add:active { background: var(--surface); }

/* ---------- Keypad ---------- */
.keypad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; flex: none; }
.keypad button {
  height: clamp(50px, 8.6dvh, 68px);
  border-radius: 14px;
  background: var(--key);
  box-shadow: var(--shadow);
  font-size: 26px; font-weight: 500; font-variant-numeric: tabular-nums;
  -webkit-user-select: none; user-select: none;
  transition: transform .06s ease, background-color .06s ease;
}
.keypad button:active { transform: scale(.96); background: var(--key-fn); }
.keypad .fn { background: var(--key-fn); font-size: 22px; }
.keypad .op { background: var(--key-op); color: var(--accent); font-size: 30px; }
.keypad .eq { background: var(--accent); color: var(--accent-ink); }

/* ---------- Bottom sheets ---------- */
dialog.sheet {
  width: 100%; max-width: 480px; max-height: 88dvh;
  margin: auto auto 0; padding: 0 0 env(safe-area-inset-bottom);
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--ink);
}
dialog.sheet[open] { display: flex; flex-direction: column; animation: sheet-up .22s ease-out; }
dialog.sheet::backdrop { background: rgba(0, 0, 0, .38); }
@keyframes sheet-up { from { transform: translateY(40%); opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  dialog.sheet[open], .row.active .val::after { animation: none; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.sheet-head h2 { margin: 0; font-size: 19px; }
.close { width: 36px; height: 36px; border-radius: 18px; background: var(--bg); font-size: 15px; color: var(--muted); }
.sheet-tools { padding: 0 16px 8px; display: flex; flex-direction: column; gap: 10px; }
.sheet-body { overflow-y: auto; padding: 0 16px 20px; -webkit-overflow-scrolling: touch; }

input[type="search"], input[type="number"] {
  font: inherit; font-size: 16px; color: var(--ink);
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  height: 44px; padding: 0 12px; width: 100%;
}
.btn { min-height: 44px; padding: 0 16px; border-radius: 12px; background: var(--bg); font-weight: 600; text-align: left; }
.btn:active { background: var(--key-fn); }
.btn.danger { color: var(--danger); }
.picker-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.picker-actions .btn { flex: 1 1 auto; }

.list-label { font-size: 12px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); margin: 14px 0 4px; }
.opt {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 52px;
  padding: 8px 4px; border-bottom: 1px solid var(--line); text-align: left;
}
.opt:active { background: var(--bg); }
.opt .flag { width: 28px; text-align: center; }
.opt .code { width: 3.4em; font-size: 16px; }
.opt .name { flex: 1; max-width: none; color: var(--muted); }
.opt.mine::after { content: "✓"; color: var(--accent); font-weight: 700; }
.empty-search { color: var(--muted); padding: 20px 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 38px; padding: 0 14px; border-radius: 19px;
  border: 1px solid var(--line); font-weight: 600; font-variant-numeric: tabular-nums;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

.price-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.price-table th { font-size: 14px; color: var(--muted); font-weight: 700; text-align: left; padding: 6px 4px; }
.price-table th:nth-child(2) { text-align: center; width: 48px; }
.price-table th:last-child, .price-table td:last-child { text-align: right; }
.price-table td { padding: 11px 4px; border-bottom: 1px solid var(--line); font-size: 19px; }
.price-table td:first-child { font-weight: 600; }
.price-table td:nth-child(2) { text-align: center; color: var(--muted); font-size: 14px; }
.flip { width: 40px; height: 32px; border-radius: 16px; background: var(--bg); font-size: 18px; color: var(--accent); }

.block { padding: 6px 0 14px; border-bottom: 1px solid var(--line); }
.block:last-child { border-bottom: 0; }
.block h3 { margin: 10px 0 6px; font-size: 16px; }
.hint { font-size: 14px; color: var(--muted); margin: 6px 0 10px; }
.fee-custom { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-weight: 600; }
.fee-custom input { width: 96px; }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 600; max-width: calc(100% - 32px); z-index: 10;
}
