:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-sunken: #fbfcfe;
  --surface-hover: #eef1f6;
  --table-head: #f8f9fc;
  --table-stripe: #fafbfd;
  --border: #e2e6ee;
  --border-strong: #c9d0dc;
  --text: #1c2433;
  --text-muted: #5f6b7e;
  --accent: #2563eb;
  --accent-soft: #dbe6ff;
  --on-accent: #ffffff;
  --danger: #c62828;
  --toast-bg: #1c2433;
  --toast-fg: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 30, 50, 0.05), 0 4px 14px rgba(20, 30, 50, 0.05);
  color-scheme: light;
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14181f;
    --surface: #1c222b;
    --surface-sunken: #171c24;
    --surface-hover: #252c37;
    --table-head: #212833;
    --table-stripe: #1f2530;
    --border: #2b3340;
    --border-strong: #3a4454;
    --text: #e7ebf2;
    --text-muted: #9aa5b6;
    --accent: #5b8def;
    --accent-soft: rgba(91, 141, 239, 0.25);
    --on-accent: #0d1320;
    --danger: #ef6b62;
    --toast-bg: #e7ebf2;
    --toast-fg: #14181f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14181f;
  --surface: #1c222b;
  --surface-sunken: #171c24;
  --surface-hover: #252c37;
  --table-head: #212833;
  --table-stripe: #1f2530;
  --border: #2b3340;
  --border-strong: #3a4454;
  --text: #e7ebf2;
  --text-muted: #9aa5b6;
  --accent: #5b8def;
  --accent-soft: rgba(91, 141, 239, 0.25);
  --on-accent: #0d1320;
  --danger: #ef6b62;
  --toast-bg: #e7ebf2;
  --toast-fg: #14181f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.5;
}

[hidden] { display: none !important; }

.container {
  max-width: 920px;
  margin: 0 auto;
  padding: 28px 24px 40px;
}

/* Header */
.page-header h1 {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* Toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin: 20px 0 14px;
}
.decimals {
  border: 0;
  margin: 0;
  padding: 0;
}
.decimals legend {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 5px;
  padding: 0;
}
.segmented {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  overflow: hidden;
}
.segmented label { position: relative; }
.segmented input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}
.segmented span {
  display: block;
  min-width: 40px;
  padding: 6px 12px;
  text-align: center;
  font-weight: 600;
  cursor: pointer;
  border-right: 1px solid var(--border);
  user-select: none;
}
.segmented label:last-child span { border-right: 0; }
.segmented input:checked + span {
  background: var(--accent);
  color: var(--on-accent);
}
.segmented input:focus-visible + span {
  outline: 3px solid var(--accent-soft);
  outline-offset: -3px;
}

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  border: 1px solid var(--border-strong);
}
.btn-secondary {
  background: var(--surface);
  color: var(--text);
}
.btn-secondary:hover { background: var(--surface-hover); }
.btn-link {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  background: none;
  border: 0;
  border-radius: 8px;
  padding: 4px 8px;
  cursor: pointer;
}
.btn-link:hover { color: var(--text); background: var(--surface-hover); }
.btn:focus-visible, .btn-copy:focus-visible, .btn-link:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: 1px;
}

/* Cards */
.cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.card.is-source { border-color: var(--accent); }
.card.is-invalid { border-color: var(--danger); }

.card-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.input-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.unit-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1.4rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  padding: 5px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-sunken);
  color: var(--text);
}
.unit-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  background: var(--surface);
}
.btn-copy {
  flex: 0 0 auto;
  width: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  color: var(--text-muted);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  cursor: pointer;
}
.btn-copy svg { width: 17px; height: 17px; }
.btn-copy:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); }
.btn-copy:disabled { opacity: 0.4; cursor: default; }

.card-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
  min-height: 1.2em;
}
.status.error { color: var(--danger); font-weight: 600; }

/* Last 10 conversions */
.history {
  margin-top: 14px;
  padding: 8px 12px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 30px;
  margin-bottom: 4px;
}
.history-title {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.history-empty {
  margin: 0;
  padding: 6px 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.history-list li + li { border-top: 1px solid var(--border); }
.history-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 8px;
  width: 100%;
  padding: 6px 8px;
  font: inherit;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
  text-align: left;
  color: var(--text-muted);
  background: none;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.history-row:hover { color: var(--text); background: var(--surface-hover); }
.history-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.history-from { font-weight: 600; color: var(--text); }
.history-to { white-space: nowrap; }
.history-arrow { margin-right: 8px; opacity: 0.6; }

/* Reference table */
.table-panel {
  margin-top: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0 20px;
}
.table-panel summary {
  cursor: pointer;
  font-size: 1rem;
  font-weight: 700;
  padding: 12px 0;
}
.table-panel[open] summary { border-bottom: 1px solid var(--border); }

.table-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 12px 0 6px;
}
.table-controls label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}
.table-controls input {
  font: inherit;
  font-size: 0.9rem;
  width: 120px;
  padding: 6px 8px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text);
  background: var(--surface);
}
.table-controls input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.table-wrap {
  overflow-x: auto;
  padding-bottom: 20px;
}
.ref-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}
.ref-table th,
.ref-table td {
  padding: 7px 12px;
  text-align: right;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.ref-table th {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--table-head);
}
.ref-table td:first-child { font-weight: 600; }
.ref-table tbody tr:nth-child(even) td { background: var(--table-stripe); }

.page-footer {
  margin-top: 18px;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

/* Copy confirmation */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  display: none;
}
.toast.show { display: block; }
.toast.error { background: var(--danger); color: #fff; }

/* Phone */
@media (max-width: 600px) {
  :root { font-size: 15px; }
  .container { padding: 18px 16px 28px; }
  .page-header h1 { font-size: 1.5rem; }
  .cards { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .unit-input { font-size: 1.3rem; }
  .toolbar { align-items: stretch; }
  .decimals, .segmented { width: 100%; }
  .segmented label { flex: 1; }
  .segmented span { min-width: 0; }
  .toolbar .btn { width: 100%; }
  .history { padding: 8px 10px 10px; }
  .history-row { font-size: 0.85rem; }
  .table-panel { padding: 0 14px; }
  .table-controls input { width: 100px; }
  .ref-table th, .ref-table td { padding: 6px 6px; font-size: 0.85rem; }
  .ref-table th { font-size: 0.72rem; letter-spacing: 0.02em; }
}
