/* Classpia Calculator — professional light/dark UI */

:root {
  --bg: #0c101c;
  --bg-grad: radial-gradient(1200px 600px at 70% -10%, #16203a 0%, #0a0d16 55%);
  --text: #f4f6fb;
  --text-sub: #b6c1d9;
  --text-faint: #7383a3;
  --border: #2a3654;

  /* Chassis (calculator body) */
  --chassis-top: #263149;
  --chassis-bot: #131a2c;
  --chassis-edge: #05070d;
  --bezel-top: #0c1220;
  --bezel-bot: #131b2e;
  --card-2: #1c2740;

  /* LCD display */
  --display-top: #10192b;
  --display-bot: #0a1120;
  --display-frame: #05080f;

  /* Keys — each has a top/bottom gradient + a solid bottom "edge" for depth.
     Tiered: util (lightest) → num (mid) → fn (darkest); op/eq are accents. */
  --num-top: #3b4870;    --num-bot: #2c3856;    --num-edge: #191f36;
  --util-top: #6b7799;   --util-bot: #58638b;   --util-edge: #2b3349;
  --fn-top: #2c3757;     --fn-bot: #202a47;     --fn-edge: #12182e;
  --op-top: #46598c;     --op-bot: #3a4b78;     --op-edge: #23304f;
  --eq-top: #ffb648;     --eq-bot: #ff9d19;     --eq-edge: #c9760a;

  --op-accent: #6f9bff;
  --key-text: #eaeffb;
  --eq-text: #3a2400;

  --shadow: 0 12px 30px rgba(0, 0, 0, 0.28);
  --radius: 26px;
  --key-radius: 15px;

  /* Keypad geometry — the .calc width is computed explicitly from these (not
     fit-content), so the fixed-width keypad can never spill outside the body,
     including on WebKit/iPad where intrinsic sizing differs from Blink. */
  --btn: 76px;          /* number-button size */
  --keypad-gap: 12px;   /* gap between keypad buttons */
  --keys-gap: 14px;     /* gap between the function column and the keypad */
  --sci-w: 216px;       /* width of the scientific function column */
  --calc-pad: 30px;     /* .calc inner padding */
}

:root[data-theme="light"] {
  --bg: #e7ebf4;
  --bg-grad: radial-gradient(1200px 600px at 70% -10%, #ffffff 0%, #dfe4f0 55%);
  --text: #131b2e;
  --text-sub: #46536f;
  --text-faint: #8b96ad;
  --border: #d3dae8;

  --chassis-top: #ffffff;
  --chassis-bot: #dfe4f1;
  --chassis-edge: #b6bfd4;
  --bezel-top: #e9edf6;
  --bezel-bot: #f4f6fb;
  --card-2: #eef1f8;

  --display-top: #e4ead8;
  --display-bot: #d7dfc9;
  --display-frame: #b9c1a8;

  --num-top: #ffffff;    --num-bot: #f0f3fa;    --num-edge: #cfd7e6;
  --util-top: #dde4f1;   --util-bot: #ccd4e4;   --util-edge: #9aa6c1;
  --fn-top: #eef1f8;     --fn-bot: #dfe5f0;     --fn-edge: #c5cddd;
  --op-top: #8ba0c8;     --op-bot: #7488b4;     --op-edge: #5a6e9a;
  --eq-top: #ffc24d;     --eq-bot: #ffa621;     --eq-edge: #e08300;

  --op-accent: #3d6bf5;
  --key-text: #1a2338;
  --eq-text: #4a2e00;

  --shadow: 0 10px 26px rgba(40, 60, 110, 0.12);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  font-family: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont,
    "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
  background: var(--bg);
  background-image: var(--bg-grad);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color .25s, color .25s;
}

.wrap { width: 100%; max-width: 900px; margin: 0 auto; padding: 0 20px; }

/* ---------- Header ---------- */
header { padding: 20px 0 4px; }
.nav { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.brand { display: flex; align-items: center; gap: 11px; text-decoration: none; }
.brand-mark {
  width: 44px; height: 44px; flex: 0 0 auto;
  border-radius: 12px; display: grid; place-items: center;
  background: linear-gradient(145deg, #22cfb8, #2f74f0);
}
.brand-mark svg { width: 26px; height: 26px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.16; min-width: 0; }
.brand-name { font-weight: 800; letter-spacing: -.02em; font-size: 17px; color: var(--text); }
.brand-sub { font-size: 11.5px; color: var(--text-sub); white-space: nowrap; }

.theme-btn {
  width: 38px; height: 38px; border-radius: 11px; cursor: pointer;
  background: var(--card-2); border: 1px solid var(--border); color: var(--text-sub);
  display: grid; place-items: center; transition: color .2s, background .2s;
}
.theme-btn:hover { color: var(--text); }
.theme-btn svg { width: 18px; height: 18px; }
:root[data-theme="light"] .icon-moon { display: none; }
:root:not([data-theme="light"]) .icon-sun { display: none; }

/* ---------- Page flow ---------- */
/* min-width:0 stops this flex item from growing to its widest child's min-content
   (which would otherwise let the calculator drag the whole page wider than the viewport). */
main { flex: 1; min-width: 0; }

/* ---------- Hero ---------- */
.hero { text-align: center; padding: 30px 20px 28px; }
.hero h1 { font-size: 30px; font-weight: 800; letter-spacing: -0.02em; line-height: 1.25; }
.hero h1 b { color: var(--op-accent); }
.hero-sub { margin-top: 10px; color: var(--text-sub); font-size: 15px; }

/* ---------- Layout ---------- */
/* Flexbox with wrap (not grid): the calculator keeps its natural width and never
   shrinks (flex:0 0 auto), so the fixed-width keypad can't spill outside the body.
   When calc + history don't fit the row, history wraps below on its own — no
   viewport breakpoint needed, so it's correct on every iPad/Safari size. */
.layout {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  gap: 20px;
  max-width: 992px;      /* wide enough for scientific (calc + history) side by side */
  padding-bottom: 8px;
}

/* ---------- Calculator chassis ---------- */
.calc {
  /* Width is computed EXPLICITLY from the keypad geometry (not fit-content), so the
     body always exactly contains the fixed-width keypad on every engine.
     Standard = 4 buttons + 3 gaps + padding. */
  width: calc(4 * var(--btn) + 3 * var(--keypad-gap) + 2 * var(--calc-pad));
  flex: 0 0 auto;         /* never grow/shrink in the flex row */
  padding: var(--calc-pad);
  background: linear-gradient(160deg, var(--chassis-top) 0%, var(--chassis-bot) 100%);
  border-radius: var(--radius);
  box-shadow:
    var(--shadow),
    0 1px 0 rgba(255, 255, 255, 0.10) inset,       /* subtle top sheen */
    0 0 0 1px var(--chassis-edge);                 /* crisp outer edge */
}
/* Scientific = function column + gap + keypad + padding. */
body.scientific .calc {
  width: calc(var(--sci-w) + var(--keys-gap) + 4 * var(--btn) + 3 * var(--keypad-gap) + 2 * var(--calc-pad));
}

.modebar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.segmented {
  display: inline-flex; border-radius: 12px; padding: 4px;
  background: linear-gradient(180deg, var(--bezel-top), var(--bezel-bot));
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06), 0 2px 6px rgba(0, 0, 0, 0.28) inset;
}
.seg {
  border: 0; background: transparent; color: var(--text-sub); cursor: pointer;
  font: inherit; font-size: 14px; font-weight: 600; padding: 7px 17px; border-radius: 9px;
  transition: background .2s, color .2s;
}
.seg.active {
  background: linear-gradient(180deg, var(--op-top), var(--op-bot)); color: #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.25) inset;
}
.angle-btn {
  border: 0; color: var(--text-sub); cursor: pointer;
  font: inherit; font-size: 12px; font-weight: 700; letter-spacing: .05em;
  padding: 9px 14px; border-radius: 11px;
  background: linear-gradient(180deg, var(--bezel-top), var(--bezel-bot));
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08), 0 2px 5px rgba(0, 0, 0, 0.25) inset;
  transition: color .2s;
}
.angle-btn:hover { color: var(--op-accent); }

/* ---------- LCD Display ---------- */
.display {
  background: linear-gradient(165deg, var(--display-top), var(--display-bot));
  border-radius: 16px;
  padding: 20px 22px;
  margin-bottom: 18px;
  min-height: 112px;
  display: flex; flex-direction: column; justify-content: flex-end;
  overflow: hidden;
  box-shadow:
    0 0 0 3px var(--display-frame),                 /* recessed frame */
    0 4px 10px rgba(0, 0, 0, 0.4) inset,            /* inner depth */
    0 1px 0 rgba(255, 255, 255, 0.05);
}
.expr {
  color: var(--text-faint); font-size: 17px; line-height: 1.4;
  min-height: 24px; white-space: nowrap; overflow-x: auto;
  font-variant-numeric: tabular-nums; scrollbar-width: none;
}
.expr::-webkit-scrollbar { display: none; }
.result {
  color: var(--text); font-size: 42px; font-weight: 700; line-height: 1.15;
  white-space: nowrap; overflow-x: auto; text-align: right;
  font-variant-numeric: tabular-nums; scrollbar-width: none;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.18);
}
.result::-webkit-scrollbar { display: none; }
.result.preview { color: var(--text-faint); font-weight: 600; text-shadow: none; }

/* ---------- Keys ---------- */
/* Standard: only the keypad, 4 square columns → tall/portrait card. */
.keys { display: flex; gap: var(--keys-gap); align-items: stretch; }
.sci-keys, .main-keys { display: grid; gap: var(--keypad-gap); }
/* Fixed keypad column width → number buttons are the same size in both modes. */
.main-keys { grid-template-columns: repeat(4, var(--btn)); flex: none; }
.sci-keys { display: none; }
/* Scientific: function column appears at left, keypad on right → wide/landscape card.
   grid-auto-rows:1fr lets the 6 function rows fill the keypad's height. */
body.scientific .sci-keys {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;
  flex: 0 0 var(--sci-w);
}

.keys button {
  /* --edge is set per-type by .num/.util/.fn/.op/.equals below.
     (Don't set it here — .keys button out-ranks those classes and would pin every
     button to one edge color, making per-type --edge changes have no effect.) */
  border: 0;
  border-radius: var(--key-radius);
  cursor: pointer;
  font-family: inherit;
  color: var(--key-text);
  font-variant-numeric: tabular-nums;
  position: relative;
  /* Per-type background is set by .num/.util/.fn/.op/.equals below.
     (No background here — .keys button would otherwise out-rank those classes.) */
  /* solid bottom "edge" makes the key look raised, plus soft cast shadow + top sheen */
  box-shadow:
    0 4px 0 var(--edge, var(--num-edge)),
    0 6px 10px rgba(0, 0, 0, 0.30),
    0 1px 0 rgba(255, 255, 255, 0.18) inset;
  transition: transform .06s ease, box-shadow .06s ease, filter .12s ease;
  user-select: none; -webkit-user-select: none;
}
.main-keys button { aspect-ratio: 1 / 1; font-size: 23px; font-weight: 600; }
.sci-keys button {
  min-height: 44px; font-size: 14px; font-weight: 600;
  box-shadow: 0 3px 0 var(--edge), 0 4px 7px rgba(0, 0, 0, 0.28), 0 1px 0 rgba(255, 255, 255, 0.15) inset;
}

.num  { --edge: var(--num-edge);  background: linear-gradient(180deg, var(--num-top), var(--num-bot)); }
.util { --edge: var(--util-edge); background: linear-gradient(180deg, var(--util-top), var(--util-bot)); color: var(--key-text); font-weight: 700; }
.fn   { --edge: var(--fn-edge);   background: linear-gradient(180deg, var(--fn-top), var(--fn-bot)); color: var(--text-sub); }
.op   { --edge: var(--op-edge);   background: linear-gradient(180deg, var(--op-top), var(--op-bot)); color: #fff; font-weight: 700; }
.equals { --edge: var(--eq-edge); background: linear-gradient(180deg, var(--eq-top), var(--eq-bot)); color: var(--eq-text); font-weight: 800; }

.keys button:hover { filter: brightness(1.06); }

/* Tactile press: key travels down and its bottom edge collapses */
.keys button:active, .keys button.pressed {
  transform: translateY(3px);
  box-shadow:
    0 1px 0 var(--edge),
    0 2px 4px rgba(0, 0, 0, 0.28),
    0 1px 0 rgba(255, 255, 255, 0.12) inset;
}
.sci-keys button:active, .sci-keys button.pressed {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--edge), 0 2px 3px rgba(0, 0, 0, 0.25) inset;
}

/* ---------- History ---------- */
.history {
  background: linear-gradient(160deg, var(--chassis-top) 0%, var(--chassis-bot) 100%);
  border-radius: var(--radius);
  box-shadow: var(--shadow), 0 1px 0 rgba(255, 255, 255, 0.08) inset, 0 0 0 1px var(--chassis-edge);
  padding: 18px;
  flex: 0 0 280px;       /* fixed width; wraps below the calc when the row is full */
  max-height: calc(100vh - 60px);
  display: flex; flex-direction: column;
}
.history-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.history-head h2 { font-size: 15px; font-weight: 700; color: var(--text); }
.ghost-btn {
  border: 0; background: transparent; color: var(--text-faint); cursor: pointer;
  font: inherit; font-size: 12px; padding: 4px 6px; border-radius: 7px; transition: color .2s;
}
.ghost-btn:hover { color: #ff6b6b; }
.history-list { list-style: none; overflow-y: auto; flex: 1; margin: 0 -6px; }
.history-item {
  padding: 10px 8px; border-radius: 10px; cursor: pointer;
  display: flex; align-items: center; gap: 6px; transition: background .15s;
  border-bottom: 1px solid var(--border);
}
.history-item:hover { background: var(--card-2); }
.h-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.h-expr { color: var(--text-faint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h-result { color: var(--text); font-size: 18px; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.h-del {
  flex: none; width: 24px; height: 24px; border: 0; border-radius: 7px;
  background: transparent; color: var(--text-faint); cursor: pointer;
  font-size: 19px; line-height: 1; display: grid; place-items: center;
  opacity: 0; transition: opacity .15s, background .15s, color .15s;
}
.history-item:hover .h-del { opacity: 1; }
.h-del:hover { background: rgba(255, 107, 107, 0.15); color: #ff6b6b; }
/* Touch devices can't hover — keep the delete button visible. */
@media (hover: none) { .h-del { opacity: 1; } }
.history-empty { color: var(--text-faint); font-size: 13px; padding: 20px 4px; text-align: center; }

/* ---------- Info section (features + shortcuts) ---------- */
.info { padding: 44px 20px 8px; display: flex; flex-direction: column; gap: 44px; }

.features { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.feature-card {
  background: var(--card-2); border: 1px solid var(--border);
  border-radius: 16px; padding: 22px;
}
.feature-card .fi {
  display: grid; place-items: center; width: 42px; height: 42px; border-radius: 11px;
  margin-bottom: 14px; color: var(--op-accent);
  background: color-mix(in srgb, var(--op-accent) 16%, transparent);
}
.feature-card .fi svg { width: 22px; height: 22px; }
.feature-card h3 { font-size: 16px; font-weight: 700; margin-bottom: 6px; }
.feature-card p { color: var(--text-sub); font-size: 14px; line-height: 1.65; }

.section-title { text-align: center; font-size: 18px; font-weight: 700; margin-bottom: 20px; }
.shortcuts { max-width: 640px; margin: 0 auto; width: 100%; }
.sc-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px 28px; }
.sc-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 4px; border-bottom: 1px solid var(--border); font-size: 14px; color: var(--text-sub);
}
.sc-keys { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
kbd {
  display: inline-block; min-width: 24px; text-align: center; padding: 3px 7px;
  border-radius: 7px; background: var(--card-2); border: 1px solid var(--border);
  border-bottom-width: 2px; color: var(--text); font-family: inherit; font-size: 12.5px; font-weight: 600;
}

/* ---------- Footer ---------- */
footer { border-top: 1px solid var(--border); padding: 22px 0 30px; margin-top: 32px; }
.foot {
  display: flex; align-items: center; justify-content: center; gap: 16px;
  color: var(--text-faint); font-size: 13px;
}
.foot a { color: var(--text-sub); text-decoration: none; }
.foot a:hover { color: var(--text); }

/* ---------- Responsive ---------- */

/* Wide screens (landscape iPad, desktop): scale the whole calculator up so it fills
   the space instead of sitting small in the middle with big side margins. */
@media (min-width: 1000px) {
  /* Bigger keypad geometry → the .calc width recomputes automatically from the vars. */
  :root { --btn: 92px; --keypad-gap: 14px; --keys-gap: 14px; --sci-w: 254px; }
  .layout { max-width: 1140px; gap: 28px; }
  .display { min-height: 128px; padding: 22px 26px; }
  .result { font-size: 50px; }
  .expr { font-size: 19px; }
  .main-keys button { font-size: 27px; }
  .sci-keys button { font-size: 16px; }
  .history { flex: 0 0 320px; }
}

/* Phones: the fixed-width keypad no longer fits, so make the whole calculator fluid —
   keypad columns flex, and the scientific function block stacks above the keypad. */
@media (max-width: 720px) {
  .result { font-size: 34px; }
  /* Stack as plain blocks so the calculator fills the viewport and its fluid keypad
     shrinks with it — avoids flex min-content keeping the card at its natural width. */
  .layout { display: block; }
  /* Override the explicit widths (base + scientific) so the card goes fluid. */
  .calc, body.scientific .calc { width: 100%; max-width: 420px; margin: 0 auto; }
  .keys, .main-keys, .sci-keys { min-width: 0; }
  .history { width: 100%; max-width: 420px; max-height: 320px; margin: 16px auto 0; }
  .main-keys { grid-template-columns: repeat(4, 1fr); flex: 1; min-width: 0; }
  body.scientific .keys { flex-direction: column; }
  body.scientific .sci-keys { grid-template-columns: repeat(5, 1fr); grid-auto-rows: auto; flex: none; }
  .hero h1 { font-size: 24px; }
  .features { grid-template-columns: 1fr; }
  .info { padding-top: 36px; }
  .sc-grid { grid-template-columns: 1fr; }
  .foot { flex-direction: column; gap: 8px; text-align: center; }
}
