/* Ganit — Indian Scientific Calculator. Original design: deep navy, warm paper display, saffron operators,
   green "=" (a quiet nod to the tricolour), big keys, natural textbook display. */
:root {
  --bg: #0b1020; --bg2: #121a33; --panel: #151e3a; --line: #26315a; --line2: #33407a;
  --text: #eef1fb; --muted: #9aa4c7; --faint: #6c769c;
  --disp: #0f1730; --disp-text: #f4f6ff; --disp-muted: #9fb0e0; --disp-line: #2a3768;
  --key: #1b2547; --key-hi: #23305c; --key-text: #eef1fb; --key-shadow: 0 1px 0 #0006, inset 0 1px 0 #ffffff10;
  --fn: #18213f; --fn-text: #c9d2f2;
  --digit: #222d55; --digit-text: #ffffff;
  --op: #ff9933; --op-text: #1b1205; --op-soft: #ff99331f;
  --eq: #27a35a; --eq-text: #ffffff;
  --del: #e0564d; --del-text: #fff;
  --accent: #ff9933; --accent2: #6ea8ff; --ok: #3fbf74; --err: #ff6b62;
  --radius: 14px; --gap: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Devanagari", sans-serif;
  --math: "STIX Two Math", "Cambria Math", "Latin Modern Math", "Noto Serif", "Times New Roman", serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, monospace;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f3f1ea; --bg2: #ebe7dc; --panel: #ffffff; --line: #ddd7c8; --line2: #cbc3ae;
  --text: #1a1f33; --muted: #5d6480; --faint: #8b90a6;
  --disp: #fbfaf4; --disp-text: #10162c; --disp-muted: #5b6488; --disp-line: #e3dece;
  --key: #ffffff; --key-hi: #f1eee4; --key-text: #1a1f33; --key-shadow: 0 1px 0 #0000001a, 0 1px 3px #0000000f;
  --fn: #e9e5d8; --fn-text: #2b3150;
  --digit: #ffffff; --digit-text: #10162c;
  --op: #f08a1c; --op-text: #ffffff; --op-soft: #f08a1c22;
  --eq: #1f8f4c; --eq-text: #ffffff;
  --del: #d64a40; --del-text: #fff;
  --accent: #d9740d; --accent2: #2f6fe0; --ok: #1f8f4c; --err: #c8392f;
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) { color-scheme: light; }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font: 15px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%; overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent2); outline-offset: 2px; }
.sr { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ shell */
#app { height: 100dvh; display: flex; flex-direction: column; max-width: 1280px; margin: 0 auto; }
.top {
  display: flex; align-items: center; gap: 6px; padding: calc(env(safe-area-inset-top) + 6px) 10px 6px; min-height: 48px;
}
.brand { flex: none; display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .2px; margin-right: auto; min-width: 0; background: none; border: 0; padding: 4px; border-radius: 10px; }
.brand img { width: 26px; height: 26px; border-radius: 7px; }
.brand .hi { color: var(--accent); font-weight: 700; }
.brand > span { white-space: nowrap; }
.brand small { color: var(--muted); font-weight: 500; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip {
  border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 999px; padding: 6px 11px; font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; white-space: nowrap;
}
.top .chip { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.top { min-width: 0; }
.chip.on { border-color: var(--accent); color: var(--accent); background: var(--op-soft); }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; border: 1px solid transparent; background: transparent; color: var(--text); display: inline-grid; place-items: center; flex: none; }
.icon-btn:hover { background: var(--panel); border-color: var(--line); }
.chip svg { flex: none; }
.icon-btn svg, .chip svg, .key svg, .btn svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.main { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr; gap: 10px; padding: 0 10px calc(env(safe-area-inset-bottom) + 8px); }
.calc { min-height: 0; display: flex; flex-direction: column; gap: 8px; }
.side { display: none; min-height: 0; }
@media (min-width: 1000px) {
  .main { grid-template-columns: minmax(0, 1fr) 360px; }
  .side { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
}

/* ------------------------------------------------------------ display */
.display {
  position: relative; background: var(--disp); color: var(--disp-text); border: 1px solid var(--disp-line); border-radius: 18px;
  padding: 10px 14px 10px; display: flex; flex-direction: column; min-height: 150px; flex: 0 0 auto;
  box-shadow: inset 0 1px 0 #ffffff0d, 0 6px 24px #00000022;
}
.status { display: flex; gap: 6px; align-items: center; font: 600 11px/1 var(--font); color: var(--disp-muted); letter-spacing: .4px; min-height: 16px; flex-wrap: wrap; }
.status .st { padding: 2px 6px; border-radius: 6px; background: #ffffff0f; }
:root[data-theme="light"] .status .st { background: #0000000a; }
.status .st.on { color: var(--accent); }
.status .spacer { margin-left: auto; }
.input {
  flex: 1; min-height: 52px; display: flex; align-items: center; overflow-x: auto; overflow-y: hidden; padding: 8px 2px; cursor: text;
  font: 24px/1.2 var(--math); outline: none; scrollbar-width: thin; touch-action: pan-x;
}
.input:focus-visible { outline: none; box-shadow: inset 0 -2px 0 var(--accent2); }
.result {
  min-height: 46px; display: flex; align-items: flex-end; justify-content: flex-end; gap: 10px; text-align: right;
  font: 600 34px/1.15 var(--math); overflow-x: auto; overflow-y: hidden; white-space: nowrap; scrollbar-width: none;
}
.result.small { font-size: 22px; }
.result.err { color: var(--err); font: 600 20px/1.3 var(--font); white-space: normal; }
.result .eqsign { color: var(--disp-muted); font-weight: 400; margin-right: auto; }
.result-lines { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.below { display: flex; gap: 6px; align-items: center; justify-content: flex-end; flex-wrap: wrap; min-height: 26px; color: var(--disp-muted); font-size: 13px; }
.below .words { margin-right: auto; color: var(--accent); font-weight: 600; }
.mini { border: 1px solid var(--disp-line); background: transparent; color: var(--disp-muted); border-radius: 8px; padding: 3px 8px; font-size: 12px; font-weight: 600; min-height: 28px; }
.mini svg { width: 14px; height: 14px; stroke: currentColor; fill: none; stroke-width: 2; vertical-align: -2px; }
.mini:hover, .mini.on { color: var(--accent); border-color: var(--accent); }
.note { color: var(--disp-muted); font-size: 12px; }

/* natural (textbook) math */
.m-row { display: inline-flex; align-items: center; white-space: nowrap; }
.m-tok { display: inline-block; padding: 0 .02em; }
.m-op { padding: 0 .16em; }
.m-fn { font-family: var(--font); font-size: .82em; padding: 0 .04em; }
.m-var { font-style: italic; }
.m-frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; margin: 0 .12em; font-size: .9em; }
.m-frac > .m-num { padding: 0 .15em .06em; border-bottom: .07em solid currentColor; min-width: .7em; justify-content: center; }
.m-frac > .m-den { padding: .06em .15em 0; min-width: .7em; justify-content: center; }
.m-sqrt { display: inline-flex; align-items: stretch; margin: 0 .06em; }
.m-sqrt > .m-rad { font-size: 1.1em; line-height: 1; display: flex; align-items: flex-end; padding-right: .02em; transform: scaleY(1.15); transform-origin: bottom; }
.m-sqrt > .m-under { border-top: .07em solid currentColor; padding: .08em .1em 0 .06em; }
.m-root { display: inline-flex; align-items: flex-start; }
.m-root > .m-index { font-size: .55em; margin-right: -.45em; margin-top: .1em; }
.m-sup { display: inline-flex; font-size: .68em; align-self: flex-start; margin-top: -.25em; margin-left: .04em; }
.m-sub { display: inline-flex; font-size: .62em; align-self: flex-end; margin-bottom: -.2em; }
.m-big { display: inline-flex; align-items: center; }
.m-big > .m-limits { display: inline-flex; flex-direction: column; align-items: center; font-size: .6em; margin: 0 .15em 0 0; }
.m-big > .m-limits > .m-sym { font-size: 2em; line-height: .9; }
.m-paren { display: inline-flex; align-items: center; }
.m-paren > .m-pl, .m-paren > .m-pr { transform: scaleY(1.25); }
.m-abs { display: inline-flex; border-left: .07em solid currentColor; border-right: .07em solid currentColor; padding: 0 .12em; margin: 0 .08em; }
.m-mat { display: inline-flex; align-items: stretch; margin: 0 .1em; }
.m-mat > .m-brk { width: .35em; border: .08em solid currentColor; }
.m-mat > .m-brk.l { border-right: 0; border-radius: .2em 0 0 .2em; }
.m-mat > .m-brk.r { border-left: 0; border-radius: 0 .2em .2em 0; }
.m-mat > table { border-collapse: collapse; font-size: .82em; }
.m-mat td { padding: .1em .45em; text-align: center; }
.m-lines { display: inline-flex; flex-direction: column; align-items: flex-end; font-size: .8em; gap: .1em; }
.m-slot { display: inline-block; min-width: .62em; min-height: .9em; border: 1px dashed var(--disp-muted); border-radius: 2px; opacity: .7; vertical-align: middle; }
.m-cursor { display: inline-block; width: 2px; height: 1.05em; background: var(--accent); margin: 0 -1px; vertical-align: middle; animation: blink 1.06s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.input:not(:focus-within):not(.focused) .m-cursor { animation: none; opacity: .55; }

/* ------------------------------------------------------------ keypad */
.pad { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; flex: none; padding: 1px; }
.tabs button { border: 1px solid var(--line); background: transparent; color: var(--muted); border-radius: 10px; padding: 6px 12px; font-weight: 600; font-size: 13px; white-space: nowrap; min-height: 34px; }
.tabs button[aria-selected="true"] { color: var(--text); background: var(--panel); border-color: var(--line2); }
.fgrid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; flex: 0 0 auto; }
.ngrid { display: grid; grid-template-columns: repeat(5, 1fr); grid-auto-rows: 1fr; gap: var(--gap); flex: 1; min-height: 200px; }
.key {
  border: 0; border-radius: 13px; background: var(--key); color: var(--key-text); box-shadow: var(--key-shadow); min-height: 44px; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2px; user-select: none; -webkit-user-select: none;
  font: 600 19px/1.1 var(--math); transition: transform .05s ease, background .1s; position: relative; touch-action: manipulation;
}
.key:active, .key.pressed { transform: translateY(1px) scale(.98); background: var(--key-hi); }
.key small { font: 600 10px/1 var(--font); color: var(--muted); margin-top: 2px; }
.fgrid .key { background: var(--fn); color: var(--fn-text); font-size: 16px; min-height: 42px; border-radius: 11px; }
.key.digit { background: var(--digit); color: var(--digit-text); font: 600 24px/1 var(--font); }
.key.op { background: var(--op-soft); color: var(--accent); font-size: 26px; }
.key.eq { background: var(--eq); color: var(--eq-text); font-size: 28px; }
.key.del { background: var(--del); color: var(--del-text); font: 700 16px/1 var(--font); }
.key.ac { background: transparent; color: var(--del); border: 1.5px solid var(--del); font: 700 16px/1 var(--font); box-shadow: none; }
.key.word { font: 700 14px/1.1 var(--font); }
.key .fn { font-family: var(--font); font-size: .86em; }
@media (hover: hover) { .key:hover { filter: brightness(1.08); } }

/* ------------------------------------------------------------ panels, sheets, forms */
.sheet-wrap { position: fixed; inset: 0; background: #0008; display: flex; align-items: flex-end; justify-content: center; z-index: 40; animation: fade .12s; }
@keyframes fade { from { opacity: 0; } }
.sheet {
  background: var(--bg); border: 1px solid var(--line); border-radius: 20px 20px 0 0; width: min(760px, 100%); max-height: min(92dvh, 900px); display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -10px 40px #0006; animation: up .16s ease-out;
}
@keyframes up { from { transform: translateY(24px); opacity: .6; } }
@media (min-width: 760px) { .sheet-wrap { align-items: center; } .sheet { border-radius: 20px; } }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 8px 16px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: 17px; margin: 0; margin-right: auto; }
.sheet-body { overflow: auto; padding: 14px 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.grid-modes { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.tile { border: 1px solid var(--line); background: var(--panel); border-radius: 14px; padding: 12px; text-align: left; display: flex; flex-direction: column; gap: 4px; min-height: 74px; }
.tile b { font-size: 14px; }
.tile span { font-size: 12px; color: var(--muted); }
.tile.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tile .n { font: 700 11px var(--mono); color: var(--accent); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > label { flex: 1 1 140px; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
input.in, select.in, textarea.in {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 10px; min-height: 42px; width: 100%; color: var(--text); font: 16px var(--font);
}
input.in:focus, select.in:focus, textarea.in:focus { border-color: var(--accent2); outline: none; }
input.num { font-family: var(--mono); }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 11px; padding: 9px 14px; font-weight: 650; min-height: 42px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1b1205; }
:root[data-theme="light"] .btn.primary { color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); border-color: var(--err); background: transparent; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 11px; overflow: hidden; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 8px 12px; font-weight: 600; min-height: 40px; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--accent); color: #1b1205; }
:root[data-theme="light"] .seg button[aria-pressed="true"] { color: #fff; }
.card { border: 1px solid var(--line); background: var(--panel); border-radius: 14px; padding: 12px; }
.out { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; align-items: baseline; }
.out dt { color: var(--muted); font-size: 13px; }
.out dd { margin: 0; text-align: right; font: 600 17px/1.3 var(--math); overflow-wrap: anywhere; }
.out dd.big { font-size: 24px; color: var(--accent); }
.hint { color: var(--muted); font-size: 12.5px; }
.warn { color: var(--err); font-size: 13px; }
.tbl-wrap { overflow: auto; max-height: 50dvh; border: 1px solid var(--line); border-radius: 12px; }
table.t { border-collapse: collapse; width: 100%; font: 13.5px var(--mono); }
table.t th, table.t td { padding: 6px 9px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
table.t th { position: sticky; top: 0; background: var(--bg2); font: 600 12px var(--font); color: var(--muted); }
table.t td:first-child, table.t th:first-child { text-align: left; }
.mat-edit { display: inline-grid; gap: 6px; }
.mat-edit input { width: 76px; text-align: center; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 18px); transform: translateX(-50%); background: var(--panel); border: 1px solid var(--line2); color: var(--text); border-radius: 12px; padding: 10px 14px; z-index: 60; box-shadow: 0 8px 30px #0006; display: flex; gap: 10px; align-items: center; max-width: calc(100% - 32px); }

/* history (side panel and sheet) */
.hist-head { display: flex; align-items: center; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.hist-head h2 { font-size: 15px; margin: 0 auto 0 0; }
.hist { list-style: none; margin: 0; padding: 6px; overflow: auto; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.hist li { border-radius: 12px; padding: 8px 10px; border: 1px solid transparent; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.hist li:hover { background: var(--bg2); border-color: var(--line); }
.hist .h-in { font: 15px var(--math); color: var(--muted); overflow-x: auto; white-space: nowrap; scrollbar-width: none; cursor: pointer; }
.hist .h-out { font: 600 19px var(--math); text-align: right; overflow-x: auto; white-space: nowrap; scrollbar-width: none; cursor: pointer; grid-column: 1; }
.hist .h-acts { grid-row: 1 / span 2; grid-column: 2; display: flex; flex-direction: column; gap: 2px; }
.hist .h-acts button { width: 30px; height: 30px; border: 0; background: transparent; color: var(--faint); border-radius: 8px; display: grid; place-items: center; }
.hist .h-acts button:hover, .hist .h-acts button.on { color: var(--accent); background: var(--op-soft); }
.hist .h-acts svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.empty { color: var(--faint); text-align: center; padding: 24px 12px; font-size: 13px; }

/* ------------------------------------------------------------ mode panels (forms instead of the keypad) */
.mode-panel { flex: 1; min-height: 0; overflow: auto; display: flex; flex-direction: column; gap: 10px; padding: 2px; }
.mode-panel .card h3 { margin: 0 0 8px; font-size: 14px; }

/* ------------------------------------------------------------ GATE practice calculator */
.gate { padding: 10px 12px calc(env(safe-area-inset-bottom) + 16px); display: flex; flex-direction: column; gap: 10px; align-items: center; }
.gate-box { width: min(560px, 100%); background: linear-gradient(180deg, #1d2847, #141c35); border: 1px solid #33407a; border-radius: 18px; padding: 12px; color: #eef1fb; box-shadow: 0 10px 40px #0005; }
:root[data-theme="light"] .gate-box { background: linear-gradient(180deg, #ffffff, #eeeae0); color: #1a1f33; border-color: #cbc3ae; }
.gate-disp { background: #f7f6ef; color: #111; border-radius: 10px; padding: 8px 10px; font: 15px var(--mono); border: 1px solid #d8d3c3; }
.gate-disp .g-expr { min-height: 20px; color: #555; overflow-x: auto; white-space: nowrap; text-align: right; font-size: 13.5px; }
.gate-disp .g-val { min-height: 34px; font: 600 26px var(--mono); text-align: right; overflow-x: auto; white-space: nowrap; }
.gate-row { display: flex; gap: 8px; align-items: center; margin: 8px 0; flex-wrap: wrap; font-size: 13px; }
.gate-box { width: min(760px, 100%); }
.gate-blocks { display: grid; grid-template-columns: 6fr 5fr; gap: 12px; }
.gate-keys { display: grid; gap: 5px; align-content: start; }
.gate-keys.left { grid-template-columns: repeat(6, 1fr); }
.gate-keys.right { grid-template-columns: repeat(5, 1fr); }
@media (max-width: 640px) { .gate-blocks { grid-template-columns: 1fr; gap: 8px; } }
.gate-keys .key { min-height: 40px; font: 600 13px/1.05 var(--font); border-radius: 8px; padding: 1px; }
.gate-keys .key.digit { font-size: 18px; }
.gate-keys .key.g-fn { background: #26325e; color: #dfe6ff; }
:root[data-theme="light"] .gate-keys .key.g-fn { background: #e4dfd0; color: #26304f; }
.gate-keys .key.g-mem { background: #2c2a52; color: #e7d9ff; }
:root[data-theme="light"] .gate-keys .key.g-mem { background: #ebe3f6; color: #3c2b5e; }

/* ------------------------------------------------------------ exam-safe mode */
body.exam .top .exam-hide { display: none !important; }
.exam-bar { display: flex; align-items: center; gap: 10px; background: #3a1712; color: #ffd9d4; border: 1px solid #7a2a20; border-radius: 12px; padding: 6px 10px; font-weight: 700; font-size: 13px; }
:root[data-theme="light"] .exam-bar { background: #fde7e3; color: #7a2a20; border-color: #e7a197; }
.exam-bar .timer { font-family: var(--mono); margin-left: auto; }
.hold { position: relative; overflow: hidden; }
.hold::after { content: ""; position: absolute; left: 0; bottom: 0; height: 3px; width: var(--p, 0%); background: currentColor; }

/* ------------------------------------------------------------ responsive */
@media (max-height: 700px) {
  .display { min-height: 128px; padding: 8px 12px; }
  .result { font-size: 30px; min-height: 40px; }
  .input { min-height: 44px; font-size: 22px; }
  .fgrid .key { min-height: 38px; }
}
@media (max-width: 520px) { .brand small, .brand .en { display: none; } }
@media (max-width: 380px) {
  .fgrid { gap: 5px; } .ngrid { gap: 6px; }
  .key.digit { font-size: 22px; }
}
.key sup { font-size: .62em; vertical-align: super; line-height: 0; }
.key sub { font-size: .62em; vertical-align: sub; line-height: 0; }
/* landscape phones and tablets: display on the left, keys on the right */
@media (orientation: landscape) and (max-height: 560px) {
  .calc { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); grid-template-rows: minmax(0, 1fr); gap: 10px; }
  .display { min-height: 0; height: 100%; }
  .pad { min-height: 0; }
  .fgrid .key { min-height: 30px; font-size: 14px; }
  .ngrid { min-height: 0; gap: 5px; }
  .key { min-height: 30px; }
  .key.digit { font-size: 19px; }
  .tabs button { min-height: 28px; padding: 3px 10px; }
  .top { min-height: 40px; padding-top: 2px; padding-bottom: 2px; }
  .below { min-height: 20px; }
}
@media (min-width: 700px) and (min-height: 700px) {
  .fgrid { grid-template-columns: repeat(8, 1fr); }
  .result { font-size: 40px; }
  .input { font-size: 28px; }
}
@media print { .pad, .top { display: none; } }
