:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #111318;
  --text-2: #50555f;
  --border: #d9dce2;
  --accent: #2456e6;
  --accent-ink: #ffffff;
  --accent-soft: #e6ecfd;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --radius: 14px;
  --gutter: 16px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --surface: #1a1d23;
    --surface-2: #23272f;
    --text: #f2f4f7;
    --text-2: #a9afba;
    --border: #343944;
    --accent: #7c9dff;
    --accent-ink: #0b1020;
    --accent-soft: #1e2a4d;
    --danger: #ff8a7a;
    --danger-soft: #3a1f1c;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  min-height: 100vh;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

#app {
  max-width: 980px;
  margin: 0 auto;
  padding-bottom: calc(32px + env(safe-area-inset-bottom));
}

/* ─── Home ─── */
.home-head {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg);
  padding: calc(14px + env(safe-area-inset-top)) var(--gutter) 10px;
}

.home-head h1 {
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: -0.02em;
  margin: 4px 0 12px;
}

.search {
  position: relative;
  display: flex;
  align-items: center;
}

.search svg {
  position: absolute;
  left: 14px;
  color: var(--text-2);
  pointer-events: none;
}

.search input {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px 10px 42px;
  font-size: 17px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  -webkit-appearance: none;
  appearance: none;
}

.search input::placeholder { color: var(--text-2); }

.home { padding: 0 var(--gutter); }

.group h2 {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-2);
  margin: 22px 2px 10px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

@media (min-width: 600px) { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 880px) { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

.tile {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  min-height: 76px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
  transition: transform 0.08s ease, border-color 0.15s ease;
}

.tile:active { transform: scale(0.98); }
@media (hover: hover) { .tile:hover { border-color: var(--accent); } }

.tile-name {
  font-weight: 700;
  font-size: 18px;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.tile-full {
  font-size: 13.5px;
  color: var(--text-2);
  line-height: 1.3;
}

.empty {
  color: var(--text-2);
  text-align: center;
  margin: 40px 0;
}

/* ─── Calculator ─── */
.bar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: grid;
  grid-template-columns: 48px 1fr 48px;
  align-items: center;
  gap: 4px;
  padding: calc(6px + env(safe-area-inset-top)) 8px 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.bar h1 {
  margin: 0;
  font-size: 19px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--accent);
}

.star svg path { fill: none; }
.star[aria-pressed="true"] { color: #e8a400; }
.star[aria-pressed="true"] svg path { fill: currentColor; }

.calc { padding: 4px var(--gutter) 0; }

.full-name {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 14px 0 2px;
}

.desc { margin: 0 0 6px; color: var(--text-2); }

.note {
  margin: 8px 0 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--accent-soft);
  font-size: 14.5px;
}

.variants { display: grid; gap: 12px; margin-top: 16px; }

.seg-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-2);
  margin: 0 2px 6px;
}

.seg {
  display: flex;
  padding: 3px;
  gap: 3px;
  border-radius: 12px;
  background: var(--surface-2);
}

.seg button {
  flex: 1 1 0;
  min-height: 44px;
  padding: 6px 8px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-weight: 600;
  font-size: 15px;
  color: var(--text-2);
}

.seg button[aria-checked="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.18);
}

.solve-for { margin-top: 18px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  min-height: 44px;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  font-size: 15px;
}

.chip[aria-checked="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.inputs { display: grid; gap: 14px; margin-top: 20px; }

.field label {
  display: block;
  font-weight: 600;
  font-size: 15px;
  margin: 0 2px 6px;
}

.control {
  position: relative;
  display: flex;
  align-items: center;
}

.control input {
  width: 100%;
  min-height: 54px;
  padding: 12px 14px;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  -webkit-appearance: none;
  appearance: none;
}

.control input::placeholder { color: var(--text-2); opacity: 0.55; }
.control.has-prefix input { padding-left: 34px; }
.control.has-suffix input { padding-right: 46px; }

.affix {
  position: absolute;
  font-size: 20px;
  color: var(--text-2);
  pointer-events: none;
}

.prefix { left: 15px; }
.suffix { right: 15px; font-size: 17px; }

.control input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
.control input[aria-invalid="true"] { border-color: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }

.result {
  margin-top: 20px;
  padding: 16px 18px 18px;
  border-radius: 18px;
  background: var(--accent);
  color: var(--accent-ink);
}

.result[data-state="error"], .result[data-state="empty"] {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.result-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.result-label { font-weight: 600; font-size: 15px; opacity: 0.9; }

.copy {
  min-height: 44px;
  min-width: 72px;
  padding: 6px 14px;
  border-radius: 10px;
  border: 1px solid currentColor;
  background: transparent;
  font-weight: 600;
  font-size: 15px;
}

.copy:disabled { opacity: 0.35; cursor: default; }

.result-value {
  display: block;
  margin-top: 4px;
  font-size: 44px;
  line-height: 1.1;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.result-msg { margin: 6px 0 0; font-size: 15px; }
.result-msg:empty { display: none; }
.result[data-state="error"] .result-msg { color: var(--danger); font-weight: 600; }
.result[data-state="empty"] .result-msg { color: var(--text-2); }

.formula {
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 14.5px;
  overflow-wrap: anywhere;
}

.formula p { margin: 0; }
.formula-nums { margin-top: 6px !important; color: var(--text-2); }
.formula-nums:empty { display: none; }

.reset {
  display: block;
  width: 100%;
  min-height: 48px;
  margin-top: 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--danger);
  font-weight: 600;
}

.noscript { padding: 24px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
