:root {
  --bg: #1a1b1e; --surface: #26282c; --surface-2: #32353a; --text: #f1f1f3; --muted: #9a9ca3; --line: #3a3d43;
  --accent: #2a55c9; --accent-text: #ffffff; --accent-soft: #24304f; --op: #3d4048; --op-text: #ffffff;
  --warn: #f0b35a; --error: #ff7b6b; --radius: 14px; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 12px calc(12px + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* Encabezado */
.top { display: flex; align-items: center; justify-content: space-between; padding: 14px 2px 10px; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; color: var(--muted); }
.icon-btn:active { background: var(--surface-2); }
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.brand:active { opacity: .7; }
.brand img { width: 34px; height: 34px; border-radius: 50%; background: #fff; }
.wordmark { display: flex; flex-direction: column; border-left: 1px solid var(--line); padding-left: 10px; line-height: 1; }
.wm-name { font-size: 1.05rem; letter-spacing: .12em; font-weight: 300; }
.wm-name b { font-weight: 700; }
.wm-name small { font-size: .7em; letter-spacing: .06em; color: var(--muted); }
.wm-sub { font-size: .6rem; letter-spacing: .32em; color: #7f9df0; margin-top: 5px; font-weight: 600; }

/* Pestañas */
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 10px; }
.tab { border: 0; background: none; padding: 8px 4px; border-radius: 9px; font-size: .92rem; color: var(--muted); font-weight: 550; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.panel { display: none; }
.panel.active { display: block; }

/* Display */
.display { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
.expr { width: 100%; border: 0; background: none; outline: none; font-size: 1.5rem; text-align: right; color: var(--text); padding: 2px 0; }
.expr::placeholder { color: var(--line); }
.round-note { text-align: right; color: var(--warn); font-size: .85rem; }
.error-msg { text-align: right; color: var(--error); font-size: .85rem; }
.error-msg:empty { display: none; }

/* Dos filas de resultado */
.results { display: grid; grid-template-columns: 1fr; gap: 6px; margin: 8px 0; }
.res { display: grid; grid-template-columns: 82px 1fr; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; text-align: right; }
.res .lbl { text-align: left; font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.res b { font-size: 2rem; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.01em; word-break: break-word; }
.res.dec b { font-size: 1.5rem; color: #b9c8f2; }
.res .round-note { grid-column: 1 / -1; }
.res:active { background: var(--surface-2); }

/* Fracciones (ventana) */
.den-row, .unit-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { flex: 0 0 auto; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: .85rem; font-variant-numeric: tabular-nums; color: var(--muted); }
.den-row .chip.active, .unit-row .chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.num-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px; max-height: 50vh; overflow-y: auto; }
.num-grid .chip { min-height: 48px; font-weight: 600; font-size: 1rem; background: var(--accent-soft); border-color: transparent; border-radius: 12px; color: var(--text); }
.num-grid .chip:active { filter: brightness(.92); }

/* Convertir (ventana) */
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.seg button { border: 0; background: none; padding: 8px; border-radius: 9px; color: var(--muted); font-weight: 600; }
.seg button.active { background: var(--surface); color: var(--text); }
.conv-out { display: grid; gap: 6px; margin-bottom: 12px; }
.conv-out:empty { display: none; }
.conv-row { display: flex; gap: 6px; }
.conv-copy { flex: 1; display: grid; grid-template-columns: 70px 1fr; align-items: baseline; text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
.conv-copy span { font-size: .75rem; color: var(--muted); text-transform: uppercase; }
.conv-copy b { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.conv-copy small { grid-column: 2; color: var(--muted); font-size: .85rem; }
.conv-use { border: 0; background: var(--accent); color: var(--accent-text); border-radius: 10px; padding: 0 14px; font-weight: 600; }
.conv-out .err { color: var(--error); }

/* Teclado */
.keypad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 7px; margin-top: 8px; }
.k { min-height: 58px; border: 0; border-radius: 12px; background: var(--surface); font-size: 1.35rem; font-weight: 500; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.k:active { filter: brightness(.9); transform: translateY(1px); }
.k.fn { background: var(--surface-2); font-size: 1.1rem; }
.k.unit { background: var(--surface-2); font-size: 1.05rem; }
.k.unit[data-unit] { font-size: .95rem; font-weight: 650; }
.k.op { background: var(--op); color: var(--op-text); font-size: 1.5rem; }
.k.eq { background: var(--accent); color: var(--accent-text); font-size: 1.6rem; font-weight: 650; }
.k.eq.tall { grid-row: span 2; }
.k.conv { background: var(--accent-soft); color: #b9c8f2; font-size: 1rem; font-weight: 700; }
.k.conv.on { background: var(--accent); color: #fff; }
.k.clr { background: #b03a2e; color: #fff; font-size: 1rem; font-weight: 700; }
.conv-hint { background: var(--accent-soft); color: var(--text); border-radius: 10px; padding: 8px 12px; font-size: .85rem; margin: 6px 0; text-align: center; }
.conv-hint[hidden] { display: none; }

/* Formularios */
.hint { color: var(--muted); font-size: .9rem; margin: 4px 2px 12px; }
.hint.small { font-size: .8rem; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: .8rem; color: var(--muted); font-weight: 550; }
.field input, .field select { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 12px; font-size: 1.1rem; min-height: 48px; }
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.div-out { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.div-out:empty { display: none; }
.div-out .part-label { font-size: .8rem; color: var(--muted); }
.div-out .part { font-size: 1.9rem; font-weight: 650; letter-spacing: -.02em; }
.div-out .sub { color: var(--muted); font-size: .9rem; margin-bottom: 10px; }
.div-out table { width: 100%; border-collapse: collapse; font-size: .92rem; font-variant-numeric: tabular-nums; }
.div-out th { text-align: left; font-size: .72rem; color: var(--muted); font-weight: 600; text-transform: uppercase; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.div-out td { padding: 7px 4px; border-bottom: 1px solid var(--line); }
.div-out td small { color: var(--muted); }
.div-out .err { color: var(--error); }

/* Historial */
.hist-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.text-btn { background: none; border: 0; color: var(--error); font-size: .9rem; padding: 8px; }
.hist-list { list-style: none; margin: 0; padding: 0; }
.hist-list li { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; display: flex; align-items: center; }
.hist-list .h-main { flex: 1; text-align: right; background: none; border: 0; padding: 10px 12px; min-width: 0; }
.hist-list .h-expr { display: block; color: var(--muted); font-size: .88rem; overflow-wrap: anywhere; }
.hist-list .h-res { display: block; font-size: 1.15rem; font-weight: 600; }
.hist-list .h-copy { border: 0; border-left: 1px solid var(--line); background: none; padding: 0 14px; align-self: stretch; color: var(--muted); font-size: .8rem; }
.hist-empty { color: var(--muted); text-align: center; padding: 30px 0; }

/* Ventanas */
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet[hidden] { display: none; }
.sheet-card { background: var(--bg); width: 100%; max-width: 560px; border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
.sheet-card h2 { margin: 0 0 12px; font-size: 1.1rem; }
.primary { width: 100%; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-text); padding: 14px; font-weight: 650; font-size: 1.05rem; }

/* Aviso */
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 999px; font-size: .9rem; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-height: 800px) { .k { min-height: 64px; } }
.top-actions { display: flex; align-items: center; gap: 4px; }
.install-btn { display: inline-flex; align-items: center; gap: 5px; color: #b9c8f2; border: 1px solid var(--line); border-radius: 999px; padding: 5px 10px; font-size: .78rem; font-weight: 600; }
.install-btn[hidden] { display: none; }