:root {
  --bg: #0f1419;
  --display: #1a2332;
  --text: #e8f0fa;
  --expr: #8fa4bf;
  --number: #243447;
  --function: #2e4158;
  --operator: #1f6feb;
  --accent: #2ecc71;
  --clear: #e67e22;
  --memory: #5b4b8a;
  --mode: #4da3ff;
  --tab-bg: #15202b;
  --tab: #243447;
  --tab-sel: #2dd4bf;
  --tab-sel-text: #0f2f2c;
  --tape: #1e2a38;
  --tape-line: #2a3d52;
  --gold: #d4af37;
  --axis: #2dd4bf;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100vh;
  padding: 10px 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
}

.logo { width: 42px; height: 42px; }

.brand h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
  color: var(--mode);
}

.brand p {
  margin: 2px 0 0;
  font-size: 0.72rem;
  color: var(--gold);
}

.top-actions { display: flex; gap: 6px; }

button {
  border: 0;
  border-radius: 12px;
  color: var(--text);
  cursor: pointer;
  font: inherit;
}

.ghost, .tiny {
  background: var(--number);
  padding: 8px 12px;
}

.tiny { font-size: 0.75rem; padding: 6px 10px; }

.primary {
  background: var(--operator);
  padding: 10px 16px;
  font-weight: 600;
}

.tape {
  background: var(--tape);
  border-radius: 16px;
  padding: 8px 10px;
  min-height: 72px;
  max-height: 120px;
  overflow: auto;
}

.tape-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--expr);
  font-size: 0.78rem;
}

.tape-list {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.78rem;
}

.tape-list li {
  border-top: 1px solid var(--tape-line);
  padding: 4px 0;
  color: var(--expr);
}

.tape-list strong { color: var(--text); font-weight: 600; }

.display {
  background: var(--display);
  border-radius: 20px;
  padding: 14px 18px 16px;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  background: color-mix(in srgb, var(--mode) 25%, transparent);
  color: var(--mode);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.75rem;
  font-weight: 650;
}

.expression {
  min-height: 1.4em;
  margin-top: 10px;
  text-align: right;
  color: var(--expr);
  word-break: break-all;
}

.result {
  margin-top: 4px;
  text-align: right;
  font-size: clamp(1.8rem, 7vw, 2.6rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  background: var(--tab-bg);
  padding: 6px;
  border-radius: 14px;
}

.tabs button {
  background: var(--tab);
  font-size: 0.72rem;
  padding: 8px 0;
  border-radius: 10px;
  min-height: 36px;
  height: 36px;
}

.tabs button.active {
  background: var(--tab-sel);
  color: var(--tab-sel-text);
  font-weight: 700;
}

.keypad { display: flex; flex-direction: column; gap: 6px; flex: 1; }

.row, .function-grid, .numeric-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.key {
  min-height: 44px;
  height: 44px;
  font-size: 0.92rem;
  font-weight: 600;
}

.key.span-2 { grid-column: span 2; }
.key.number { background: var(--number); }
.key.function { background: var(--function); }
.key.operator { background: var(--operator); }
.key.memory { background: var(--memory); }
.key.mode { background: var(--function); color: var(--mode); }
.key.clear { background: var(--clear); }
.key.equals { background: var(--accent); color: #073b22; }

.pager {
  background: color-mix(in srgb, var(--function) 35%, transparent);
  border-radius: 14px;
  padding: 8px;
}

.pager-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--expr);
  font-size: 0.78rem;
  margin-bottom: 6px;
}

.dots { display: flex; gap: 5px; }

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--tape-line);
  cursor: pointer;
}

.dot.active { background: var(--tab-sel); }

.modal {
  border: 0;
  padding: 0;
  background: transparent;
  color: inherit;
}

.modal::backdrop { background: rgba(0, 0, 0, 0.65); }

.modal-card {
  width: min(92vw, 420px);
  background: var(--display);
  border-radius: 20px;
  padding: 22px;
  text-align: center;
}

.gold { color: var(--gold); }
.muted { color: var(--expr); font-size: 0.9rem; }

.plot-modal .plot-card {
  width: min(96vw, 980px);
  text-align: left;
}

.plot-toolbar, .plot-range {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.plot-expr, .plot-range label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 0;
}

input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--tape-line);
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
}

.plot-expr input { flex: 1; }

#plot-canvas {
  width: 100%;
  height: auto;
  background: #0b1117;
  border-radius: 12px;
  border: 1px solid var(--tape-line);
}

@media (min-width: 980px) {
  .app {
    max-width: 1100px;
    display: grid;
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-rows: auto auto auto auto;
    align-items: start;
    gap: 12px;
  }
  .topbar { grid-column: 1 / -1; grid-row: 1; }
  .tape {
    grid-column: 1;
    grid-row: 2 / 5;
    max-height: none;
    height: 100%;
  }
  .display { grid-column: 2; grid-row: 2; }
  .tabs { grid-column: 2; grid-row: 3; }
  .keypad { grid-column: 2; grid-row: 4; }
}

/* Web ergonomics and desktop tape layout. */
#numeric-pad { display: flex; flex-direction: column; gap: 6px; }
button:hover { filter: brightness(1.15); }
button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--tab-sel); outline-offset: 3px; }
.mode-help { color: var(--expr); font-size: .78rem; line-height: 1.5; margin: 0; min-height: 2.5em; }
select { background: var(--bg); color: var(--text); padding: 8px; border: 1px solid var(--tape-line); border-radius: 8px; font: inherit; }
.dot { width: 28px; height: 28px; padding: 0; border-radius: 8px; }
.tape-list li { padding: 10px 0; overflow-wrap: anywhere; line-height: 1.6; }
.logo { flex-shrink: 0; }
.plot-expr input { min-width: 0; }
.plot-range input { width: 110px; }
@media (max-width: 420px) {
  .topbar { flex-wrap: wrap; }
  .top-actions { margin-left: auto; }
  .key { font-size: .8rem; }
  .plot-expr { flex-wrap: wrap; }
}
@media (min-width: 980px) {
  .app { padding-top: 28px; grid-template-rows: auto auto auto auto auto; }
  .tape { grid-row: 2 / 6; max-height: 760px; }
  .mode-help { grid-column: 2; grid-row: 4; }
  .keypad { grid-row: 5; }
  .key { height: 52px; }
  .topbar { margin-bottom: 16px; }
}
@media print {
  html, body { background: white; color: black; }
  .app { display: block; max-width: none; }
  .app > :not(.topbar):not(.tape), .top-actions, .tape-head button, dialog { display: none !important; }
  .tape { background: white; height: auto; max-height: none; overflow: visible; }
  .tape-list li, .tape-list strong, .tape-head, .brand h1, .brand p { color: black; }
}

.wide-modal { width: min(1200px, 98vw); max-width: 98vw; max-height: 96dvh; }
.workspace-card { width: 100%; text-align: left; }
.modal { max-height: 96dvh; border-radius: 20px; }
.modal-card { max-width: 100%; }
.tool-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin: 12px 0; }
.tool-controls label { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: .85rem; color: var(--expr); }
.tool-controls input:not([type=checkbox]):not([type=range]):not([type=color]), .tool-controls select { max-width: min(260px, 75vw); }
.tool-controls input[type=number] { width: 115px; }
.tool-controls input[type=color] { width: 42px; height: 32px; padding: 2px; }
.tool-controls input[type=range] { width: 135px; }
.tool-controls [aria-pressed=true] { outline: 2px solid var(--tab-sel); }
.astro-cards { display: grid; grid-template-columns: repeat(auto-fit,minmax(210px,1fr)); gap: 12px; }
.astro-card { background: var(--tab-bg); border: 1px solid var(--tape-line); border-radius: 14px; padding: 14px; }
.astro-card h4 { margin: 0; color: var(--expr); font-size: .85rem; font-weight: 500; }
.astro-card p { color: var(--text); margin: 8px 0; font-size: 1.1rem; }
.astro-card small { color: var(--expr); }
.city-results { display: flex; flex-wrap: wrap; gap: 6px; max-height: 140px; overflow: auto; margin: 10px 0; }
.location-settings { background: var(--tab-bg); border: 1px solid var(--tape-line); padding: 12px; border-radius: 12px; }
summary { cursor: pointer; color: var(--mode); }
#sky-canvas, #qibla-canvas { display: block; width: 100%; height: auto; max-height: 75dvh; object-fit: contain; background: #0b1117; border-radius: 12px; }
#sky-canvas, #plot-canvas { touch-action: none; cursor: grab; }
#sky-canvas:active, #plot-canvas:active { cursor: grabbing; }
#sky-guide { display: block; border: 0; width: 100%; height: 440px; margin-top: 12px; }
.top-actions { flex-wrap: wrap; justify-content: flex-end; }
[hidden] { display: none !important; }
#astro-status { color: var(--tab-sel); }
#plot-legend span { font: .85rem ui-monospace,monospace; }
@media (max-width:600px) {
  .workspace-card { padding: 12px; }
  .tool-controls { gap: 8px; }
  .tool-controls input, .tool-controls select { min-height: 40px; }
  .astro-cards { grid-template-columns: 1fr 1fr; gap: 6px; }
  .astro-card { padding: 10px; min-width: 0; overflow-wrap: anywhere; }
  .astro-card p { font-size: .95rem; }
  .topbar { flex-wrap: wrap; }
}
@media print {
  body[data-print=plot] .app { display: none !important; }
  body[data-print=plot] #plot-dialog[open] { display: block !important; position: static; width: 100%; max-width: none; max-height: none; margin: 0; }
  body[data-print=plot] .plot-card { width: 100%; max-width: none; padding: 0; background: white; color: black; }
  body[data-print=plot] .plot-card > .workspace-header { display: none !important; }
  body[data-print=plot] #plot-legend { color: black; }
  body[data-print=plot] #plot-legend input { display: none; }
}

/* Shared controls panels for graphing and astronomy. */
.control-dialog { width: min(1440px, 98vw); max-width: 98vw; height: min(980px, 96dvh); overflow: hidden; }
.control-dialog .control-card { width: 100%; height: 100%; display: flex; flex-direction: column; padding: 16px; text-align: left; }
.workspace-header { flex: 0 0 auto; flex-wrap: nowrap; gap: 12px; padding-bottom: 14px; }
.workspace-header h2 { flex: 1; margin: 0; font-size: 1.25rem; }
.workspace-layout { position: relative; display: grid; grid-template-columns: 310px minmax(0, 1fr); gap: 16px; flex: 1; min-height: 0; }
.workspace-layout[data-collapsed=true] { grid-template-columns: minmax(0, 1fr); }
.workspace-sidebar { min-width: 0; overflow-y: auto; overscroll-behavior: contain; background: var(--tab-bg); border: 1px solid var(--tape-line); border-radius: 14px; padding: 14px; scrollbar-width: thin; }
.sidebar-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 14px; }
.sidebar-heading h2 { font-size: 1rem; margin: 0; }
.workspace-sidebar h3 { font-size: .85rem; color: var(--mode); margin: 18px 0 10px; }
.workspace-sidebar .tool-controls, .workspace-sidebar .plot-range, .workspace-sidebar .plot-expr { display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-start; }
.workspace-sidebar .tool-controls label:not(:has(input[type=checkbox])) { display: flex; flex-direction: column; align-items: stretch; gap: 5px; width: 100%; }
.workspace-sidebar .tool-controls input:not([type=checkbox]):not([type=color]), .workspace-sidebar .tool-controls select, .workspace-sidebar .plot-range input { width: 100%; max-width: 100%; min-width: 0; }
.workspace-sidebar .plot-expr { flex-direction: column; align-items: stretch; }
.workspace-sidebar .plot-range label { width: 100%; justify-content: space-between; }
.workspace-sidebar .plot-range label input, .workspace-sidebar .plot-range label select { width: 65%; }
.workspace-sidebar .location-settings { padding: 0; border: 0; background: transparent; }
.workspace-sidebar .city-results { flex-direction: column; flex-wrap: nowrap; max-height: 200px; }
.workspace-sidebar .city-results button { text-align: left; flex-shrink: 0; }
.workspace-sidebar .control-group { border-top: 1px solid var(--tape-line); margin-top: 18px; }
.workspace-sidebar .tool-controls { border-bottom: 1px solid var(--tape-line); padding-bottom: 12px; }
.workspace-content { min-width: 0; overflow: auto; overscroll-behavior: contain; scrollbar-width: thin; }
.workspace-content > :first-child { margin-top: 0; }
.workspace-content #plot-canvas { display: block; }
.workspace-content #sky-canvas { max-height: none; }
.sidebar-backdrop { position: absolute; inset: 0; background: #0008; border-radius: 0; z-index: 1; }
@media (max-width: 760px) {
  .control-dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  .control-dialog .control-card { padding: 12px; border-radius: 0; }
  .workspace-layout { display: block; }
  .workspace-sidebar { position: absolute; inset: 0 auto 0 0; width: min(340px, calc(100% - 24px)); z-index: 2; box-shadow: 12px 0 30px #0006; }
  .workspace-content { height: 100%; }
  .workspace-header { gap: 8px; }
  .workspace-header h2 { font-size: 1rem; }
  .workspace-header button { min-height: 40px; padding: 8px; }
}
@media print {
  body[data-print=plot] #plot-dialog .control-card, body[data-print=plot] #plot-dialog .workspace-layout, body[data-print=plot] #plot-dialog .workspace-content { display: block !important; height: auto; overflow: visible; width: 100%; }
  body[data-print=plot] #plot-dialog .workspace-header, body[data-print=plot] #plot-dialog .workspace-sidebar, body[data-print=plot] #plot-dialog .sidebar-backdrop { display: none !important; }
  body[data-print=plot] #plot-dialog .workspace-content > :not(canvas) { display: none !important; }
}

/* Compact phone calculator: leave room for every numeric row below browser chrome. */
.tape-toggle { display: none; }
@media screen and (max-width: 760px) {
  .app { min-height: 100dvh; padding: 6px 8px max(8px, env(safe-area-inset-bottom)); gap: 4px; }
  .topbar { flex-wrap: wrap; gap: 4px; }
  .brand { gap: 7px; min-width: 0; }
  .logo { width: 28px; height: 28px; }
  .brand h1 { font-size: .9rem; }
  .brand p { font-size: .6rem; margin-top: 0; }
  .top-actions { width: 100%; margin: 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  .top-actions .ghost { padding: 5px 3px; min-height: 30px; font-size: .72rem; border-radius: 9px; }
  .tape { min-height: 0; max-height: none; padding: 3px 8px; border-radius: 10px; overflow: visible; }
  .tape-desktop-title { display: none; }
  .tape-toggle { display: inline-block; background: transparent; color: var(--expr); font-size: .75rem; padding: 4px 0; text-align: left; }
  .tape-head .tiny { padding: 4px 8px; min-height: 26px; font-size: .68rem; }
  .app:not(.tape-expanded) .tape-list { display: none; }
  .tape-list { max-height: 110px; overflow-y: auto; margin-top: 3px; }
  .tape-list li { padding: 5px 0; font-size: .7rem; }
  .display { padding: 6px 12px; border-radius: 12px; }
  .chip { padding: 2px 7px; font-size: .65rem; }
  .expression { margin-top: 3px; min-height: 1.2em; max-height: 2.4em; overflow-y: auto; font-size: .8rem; }
  .result { margin-top: 0; font-size: 1.8rem; line-height: 1.15; }
  .tabs { padding: 3px; gap: 3px; border-radius: 10px; }
  .tabs button { height: 30px; min-height: 30px; padding: 4px 0; font-size: .67rem; border-radius: 8px; }
  .mode-help { font-size: .65rem; line-height: 1.3; min-height: 0; margin: 0; }
  .keypad { flex: 0 0 auto; gap: 4px; }
  .row, .function-grid, .numeric-row { gap: 4px; }
  .key { height: 40px; min-height: 40px; border-radius: 9px; font-size: .85rem; }
  .pager { padding: 4px; border-radius: 10px; }
  .pager-head { margin-bottom: 3px; min-height: 20px; font-size: .7rem; }
  .dot { width: 22px; height: 20px; border-radius: 6px; }
  .function-grid .key, #memory-row .key { height: 36px; min-height: 36px; font-size: .77rem; }
  #numeric-pad { gap: 4px; }
  .numeric-row .key { height: 44px; min-height: 44px; font-size: 1rem; }
}
@media screen and (max-width: 760px) and (max-height: 700px) {
  .mode-help { display: none; }
}
@media screen and (max-width: 760px) and (max-height: 620px) {
  .key { height: 36px; min-height: 36px; }
  .function-grid .key, #memory-row .key { height: 32px; min-height: 32px; }
  .numeric-row .key { height: 40px; min-height: 40px; }
}
@media print {
  .tape-toggle { display: none !important; }
  .tape-desktop-title { display: inline !important; }
  .app .tape-list { display: block !important; max-height: none; overflow: visible; }
}
