/* Calculator PWA — responsive, touch-first, two themes. */
:root {
  --bg: #202020;
  --surface: #202020;
  --surface-2: #2b2b2b;
  --keypad: #323232;
  --text: #f3f3f3;
  --muted: #a6a6a6;
  --accent: #60cdff;
  --accent-text: #102030;
  --op: #f3f3f3;
  --fn: #d8d8d8;
  --border: #3a3a3d;
  --shadow: 0 8px 32px rgba(0, 0, 0, .45);
  --panel-w: 100%;
}

html[data-theme="light"] {
  --bg: #f3f3f3;
  --surface: #f3f3f3;
  --surface-2: #e6e6e6;
  --keypad: #ebebeb;
  --text: #111114;
  --muted: #6a6a6a;
  --accent: #0078d4;
  --accent-text: #ffffff;
  --op: #111114;
  --fn: #333333;
  --border: #d5d5da;
  --shadow: 0 8px 32px rgba(0, 0, 0, .12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

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

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 100vh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  padding: 4px 14px;
  background: var(--bg);
}
#menuBtn { margin-left: -8px; }
#title {
  margin: 0;
  flex: 1;
  font-size: clamp(20px, 5vw, 26px);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions { display: flex; gap: 4px; }
#topbar .icon-btn { border: 0; background: transparent; min-width: 40px; }
#topbar .icon-btn:hover { background: var(--surface-2); }

.icon-btn, .mini-btn, .chip, .seg {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 12px;
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px;
  font-size: 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .12s ease, transform .06s ease;
}

.icon-btn:active, .mini-btn:active, .chip:active, .seg:active,
.key:active, .nav-item:active, .base-row:active {
  transform: scale(.96);
  background: var(--border);
}

.icon-btn:hover, .mini-btn:hover, .chip:hover { background: var(--border); }

#shell { position: relative; display: flex; flex: 1; min-height: 0; }

#drawer {
  position: absolute;
  z-index: 20;
  inset: 0 auto 0 0;
  width: min(84vw, 300px);
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 12px calc(14px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: var(--shadow);
  transform: translateX(-102%);
  transition: transform .18s ease;
  overflow-y: auto;
}

#drawer.open { transform: none; }
#drawerScrim { position: absolute; inset: 0; z-index: 19; background: rgba(0, 0, 0, .5); }

.drawer-head {
  font-size: 22px;
  font-weight: 700;
  padding: 8px 10px 16px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 52px;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 16px;
  text-align: left;
}
.nav-item span { width: 26px; text-align: center; color: var(--muted); font-size: 20px; }
.nav-item.active { background: var(--surface-2); color: var(--accent); }
.nav-item.active span { color: var(--accent); }
.drawer-divider { height: 1px; background: var(--border); margin: 10px 8px; }
.drawer-foot { margin-top: auto; padding: 16px 12px 0; color: var(--muted); font-size: 12px; }

#main {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.view {
  width: min(100%, var(--panel-w));
  margin: 0 auto;
  padding: 10px 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 100%;
}
.view[hidden] { display: none; }
/* Calculator modes use the whole screen; only form-style views stay narrow. */
body[data-mode="standard"] .view,
body[data-mode="scientific"] .view,
body[data-mode="programmer"] .view { width: 100%; }
body[data-mode="date"] .view,
body[data-mode="rational"] .view,
body[data-mode="mulshift"] .view,
body[data-mode="history"] .view,
body[data-mode="memory"] .view,
body[data-mode="settings"] .view { width: min(100%, 560px); }

/* Calculator modes fill the viewport: a large display, a slim memory row,
   then a keypad whose rows grow to consume the remaining height. */
body[data-mode="standard"] #calcView,
body[data-mode="scientific"] #calcView { height: 100%; }

.display {
  position: relative;
  flex: 0 0 clamp(140px, 28vh, 260px);
  padding: 16px 18px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
  gap: 4px;
  background: transparent;
  overflow: hidden;
}

.mem-bar {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  align-items: center;
  min-height: 36px;
}
.mem-bar[hidden] { display: none; }
.mem-bar button {
  border: 0;
  background: transparent;
  color: var(--muted);
  min-height: 36px;
  padding: 0 2px;
  font-size: clamp(11px, 3vw, 13px);
  border-radius: 8px;
}
.mem-bar button:active { background: var(--surface-2); color: var(--text); }
.mem-bar button.has-memory { color: var(--accent); }

.group-toggle {
  position: absolute;
  left: 14px;
  top: 10px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: clamp(12px, 2vw, 16px);
  padding: 4px 6px;
  border-radius: 6px;
}
.group-toggle:hover { background: var(--surface-2); color: var(--text); }

.expression {
  width: 100%;
  min-height: 24px;
  color: var(--muted);
  font-size: clamp(14px, 4vw, 18px);
  text-align: right;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.result {
  width: 100%;
  font-size: clamp(42px, 11vw, 96px);
  font-weight: 500;
  letter-spacing: -1px;
  text-align: right;
  line-height: 1.12;
  direction: ltr;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  white-space: nowrap;
  outline: none;
}
.result::-webkit-scrollbar { display: none; }
.result.error { color: var(--op); font-size: clamp(20px, 6vw, 32px); }
.mem-badge {
  position: absolute; left: 16px; top: 40px;
  color: var(--accent); font-size: 14px; font-weight: 700;
}

.word-row, .segmented {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip, .seg { min-height: 40px; font-size: 13px; border-radius: 10px; }
.chip.active, .seg.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.base-display {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.base-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.base-row.active { border-color: var(--accent); }
.base-label { color: var(--muted); font-size: 12px; font-weight: 700; }
.base-value {
  flex: 1;
  min-width: 0;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: clamp(14px, 4vw, 18px);
  text-align: right;
  overflow-x: auto;
  scrollbar-width: none;
  white-space: nowrap;
}
.base-value::-webkit-scrollbar { display: none; }

.bit-panel {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 4px;
  max-height: 190px;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.bit {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  min-height: 34px;
  font-family: ui-monospace, monospace;
  font-size: 12px;
}
.bit.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.bit-label { color: var(--muted); font-size: 9px; }

.ops-toggle {
  flex: 0 0 auto;
  align-self: stretch;
  min-height: 34px;
  border: 0;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: clamp(13px, 2vw, 16px);
}
.ops-toggle[hidden] { display: none; }
.ops-panel {
  flex: 0 0 auto;
  max-height: 34vh;
  overflow-y: auto;
  padding: 8px;
  border-radius: 10px;
  background: var(--surface-2);
}
.ops-panel[hidden] { display: none; }
.ops-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(clamp(64px, 12vw, 92px), 1fr));
  gap: 6px;
}
.ops-grid .key { min-height: 42px; font-size: clamp(12px, 2vw, 16px); border-radius: 6px; }

.keypad {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-auto-rows: minmax(44px, 1fr);
  gap: 8px;
  min-height: 0;
}
.keypad.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }

.key {
  min-height: 0;
  border: 0;
  border-radius: 8px;
  background: var(--keypad);
  color: var(--text);
  font-size: clamp(20px, 3vw, 36px);
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .1s ease, transform .06s ease;
}
.key:hover { background: var(--surface-2); }
.key.fn { color: var(--fn); font-size: clamp(14px, 2vw, 26px); }
.key.op { color: var(--op); background: var(--surface-2); }
.key.op.active { background: var(--accent); color: var(--accent-text); }
.key.equals { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.key.wide { grid-column: span 2; }
.key.dim { opacity: .35; pointer-events: none; }

/* Programmer packs many panels into one screen, so it scrolls instead of
   shrinking its bit controls below touch size. */
body[data-mode="scientific"] .display { flex-basis: clamp(110px, 20vh, 180px); }
body[data-mode="scientific"] .keypad { grid-auto-rows: minmax(40px, 1fr); }

body[data-mode="programmer"] #calcView { height: auto; min-height: 100%; }
body[data-mode="programmer"] .display { flex-basis: 130px; }
body[data-mode="programmer"] .keypad {
  flex: 0 0 auto;
  grid-auto-rows: 52px;
}

.view-title { margin: 4px 0; font-size: 20px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; }

.date-form, .field { display: flex; flex-direction: column; gap: 6px; }
.date-form { gap: 14px; }
.date-form label, .field { color: var(--muted); font-size: 13px; }
input[type="date"], input[type="number"], input[type="text"], select {
  width: 100%;
  min-height: 48px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  -webkit-user-select: text;
  user-select: text;
}
input:focus, select:focus, button:focus-visible, .result:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.triple { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.conv-pair { display: grid; grid-template-columns: 1fr auto 1fr; align-items: end; gap: 8px; }
.swap { margin-bottom: 2px; }
.date-result {
  min-height: 64px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  font-size: clamp(18px, 5vw, 24px);
  text-align: right;
  direction: ltr;
  overflow-wrap: anywhere;
}
.conv-all { display: grid; gap: 6px; }
.conv-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
}
.conv-row span:last-child { font-family: ui-monospace, monospace; direction: ltr; }

.record-list { display: grid; gap: 8px; }
.record {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  text-align: right;
  width: 100%;
}
.record .record-expression { color: var(--muted); font-size: 13px; }
.record .record-result { font-size: 20px; direction: ltr; }
.record .record-date { color: var(--muted); font-size: 11px; }
.empty { padding: 40px 0; text-align: center; color: var(--muted); }
.check { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.hint { color: var(--muted); font-size: 13px; line-height: 1.5; }
.record-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.record-actions .mini-btn { min-height: 36px; min-width: 44px; font-size: 13px; }
.code-block {
  margin: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre;
  -webkit-user-select: text;
  user-select: text;
}

.status { display: none; }

/* The nav is always an overlay, so it never steals space from the keys.
   On wide screens the calculator fills the available area edge to edge. */
@media (min-width: 860px) {
  #drawer { width: min(84vw, 320px); }
  #main { padding: 0; }
  .view { padding: 10px 16px 14px; }
  .display { flex-basis: clamp(180px, 32vh, 340px); }
}

@media (max-width: 400px) {
  .view { padding: 8px 8px 10px; }
  .keypad { gap: 6px; }
  .key { border-radius: 9px; font-size: 17px; }
  .key.fn { font-size: 12px; }
  .display { padding: 12px; }
}

@media (orientation: landscape) and (max-height: 520px) {
  #topbar { min-height: 44px; }
  .display,
  body[data-mode="programmer"] .display { flex-basis: 92px; padding: 8px 12px; }
  .result { font-size: clamp(26px, 6vh, 38px); }
  .keypad,
  body[data-mode="programmer"] .keypad { grid-auto-rows: 40px; }
  .key { border-radius: 8px; }
  .view { gap: 6px; padding-top: 6px; }
  .mem-bar { min-height: 28px; }
  .bit-panel { max-height: 88px; }
  .status { display: none; }
}

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