/* ==========================================================================
   1. Design tokens (CSS custom properties)
   Mọi màu đều đi qua biến → đổi theme chỉ cần định nghĩa lại biến.
   ========================================================================== */
:root {
  --page-bg: #eef2f7;
  --page-bg-top: #f7f9fc;
  --page-glow: rgba(37, 88, 199, .10);
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --surface-glass: rgba(255, 255, 255, .96);
  --text: #172033;
  --text-strong: #111827;
  --text-soft: #475569;
  --muted: #667085;
  --line: #d8e0ea;
  --line-strong: #cbd5e1;
  --accent: #2558c7;
  --accent-hover: #dce7ff;
  --accent-soft: #e9f0ff;
  --accent-line: #c9d5f2;
  --accent-strong: #173e99;
  --on-accent: #ffffff;
  --danger: #a83535;
  --danger-soft: #fff0f0;
  --danger-line: #f0caca;
  --success: #26734d;
  --success-soft: #edf9f2;
  --success-line: #cfe7da;
  --result: #1f6f4a;
  --key-bg: #f8fafc;
  --key-hover: #f0f4f9;
  --key-line: #d2dbe7;
  --key-control-bg: #f3f4f6;
  --key-control-text: #374151;
  --focus-ring: rgba(37, 88, 199, .35);
  --shadow: 0 18px 45px rgba(18, 38, 63, 0.12);
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --gap: 10px;
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* Dark theme: cùng tên biến, giá trị khác. data-theme do JS đặt lên <html>. */
:root[data-theme="dark"] {
  --page-bg: #0b1220;
  --page-bg-top: #0f172a;
  --page-glow: rgba(96, 140, 255, .16);
  --surface: #111a2e;
  --surface-soft: #16213a;
  --surface-glass: rgba(17, 26, 46, .96);
  --text: #e5ebf5;
  --text-strong: #f8fafc;
  --text-soft: #b6c2d6;
  --muted: #94a3b8;
  --line: #26344f;
  --line-strong: #33446a;
  --accent: #6d9bff;
  --accent-hover: #24365f;
  --accent-soft: #1b2a4a;
  --accent-line: #2f4577;
  --accent-strong: #a9c3ff;
  --on-accent: #0b1220;
  --danger: #ff8a8a;
  --danger-soft: #3a1c22;
  --danger-line: #5c2a33;
  --success: #6ee0a6;
  --success-soft: #133326;
  --success-line: #1f4d39;
  --result: #6ee0a6;
  --key-bg: #17223a;
  --key-hover: #1e2b48;
  --key-line: #2a3a5a;
  --key-control-bg: #1c2438;
  --key-control-text: #cbd5e1;
  --focus-ring: rgba(109, 155, 255, .55);
  --shadow: 0 18px 45px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

/* ==========================================================================
   2. Base
   ========================================================================== */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    radial-gradient(circle at 20% 0%, var(--page-glow), transparent 28rem),
    linear-gradient(180deg, var(--page-bg-top) 0%, var(--page-bg) 100%);
  color: var(--text);
  transition: background-color .2s ease, color .2s ease;
}

button,
input,
output {
  font: inherit;
}

h1,
h2,
p {
  margin-top: 0;
}

kbd,
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: .85em;
}

kbd {
  display: inline-block;
  padding: 0 5px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-soft);
  color: var(--text-soft);
  line-height: 1.5;
}

[hidden] {
  display: none !important;
}

/* ==========================================================================
   3. Layout
   ========================================================================== */
.app-shell {
  width: min(100% - 28px, 820px);
  margin: 0 auto;
  padding: 36px 0 28px;
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--accent);
  font-size: .75rem;
  font-weight: 800;
  letter-spacing: .12em;
}

h1 {
  margin-bottom: 3px;
  font-size: clamp(2rem, 7vw, 3.25rem);
  line-height: .98;
  letter-spacing: -.04em;
}

.subtitle {
  margin-bottom: 0;
  color: var(--muted);
  font-weight: 650;
}

.version-badge,
.status-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-glass);
  padding: 7px 10px;
  color: var(--accent-strong);
  font-size: .78rem;
  font-weight: 800;
  white-space: nowrap;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-glass);
  color: var(--accent-strong);
  cursor: pointer;
  font-size: 1.1rem;
}

.icon-button:hover {
  border-color: var(--accent);
}

.learning-goal {
  margin-bottom: 16px;
  padding: 13px 15px;
  border: 1px solid var(--accent-line);
  border-radius: var(--radius-md);
  background: var(--accent-soft);
}

.learning-goal h2 {
  margin-bottom: 4px;
  font-size: .95rem;
}

.learning-goal p {
  margin-bottom: 0;
  color: var(--text-soft);
  font-size: .9rem;
  line-height: 1.5;
}

/* Calculator + History: xếp chồng trên mobile, 2 cột trên màn hình rộng. */
.workspace {
  display: grid;
  gap: 18px;
  align-items: start;
}

@media (min-width: 760px) {
  .workspace {
    grid-template-columns: minmax(0, 430px) minmax(0, 1fr);
  }
}

.calculator-card,
.history-card,
.learning-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface-glass);
  box-shadow: var(--shadow);
}

.calculator-card {
  width: min(100%, 430px);
  margin: 0 auto;
  padding: 18px;
}

.calculator-title-row,
.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.calculator-title-row h2,
.history-header h2 {
  margin-bottom: 0;
  font-size: 1.06rem;
}

.status-badge {
  border-color: var(--success-line);
  background: var(--success-soft);
  color: var(--success);
  padding: 5px 9px;
  font-size: .72rem;
}

.status-badge.error {
  border-color: var(--danger-line);
  background: var(--danger-soft);
  color: var(--danger);
}

/* ==========================================================================
   4. Display
   ========================================================================== */
.display-panel {
  overflow: hidden;
  margin-bottom: 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  background: linear-gradient(180deg, var(--surface-soft) 0%, var(--surface) 100%);
}

.expression,
.main-display {
  min-width: 0;
  overflow: hidden;
  text-align: right;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.expression {
  min-height: 40px;
  padding: 12px 14px 5px;
  color: var(--muted);
  font-size: .92rem;
  font-weight: 650;
}

.display-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 14px 12px 10px;
}

.main-display {
  flex: 1;
  min-height: 60px;
  color: var(--text-strong);
  font-size: clamp(2.25rem, 11vw, 3.4rem);
  font-variant-numeric: tabular-nums;
  font-weight: 750;
  letter-spacing: -.045em;
  line-height: 60px;
}

/* Số dài → chữ nhỏ lại thay vì bị cắt. JS đặt data-size theo độ dài chuỗi. */
.main-display[data-size="md"] {
  font-size: clamp(1.7rem, 8vw, 2.5rem);
}

.main-display[data-size="sm"] {
  font-size: clamp(1.2rem, 5.6vw, 1.75rem);
  letter-spacing: -.02em;
}

.main-display.is-result {
  color: var(--result);
  animation: result-pop .22s ease-out;
}

.main-display.is-error {
  color: var(--danger);
  font-size: clamp(1.7rem, 8vw, 2.3rem);
}

@keyframes result-pop {
  from {
    transform: scale(.94);
    opacity: .4;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

.copy-button {
  flex: none;
  width: 32px;
  height: 32px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
}

.copy-button:hover:not(:disabled) {
  border-color: var(--line);
  background: var(--surface-soft);
  color: var(--accent);
}

.copy-button:disabled {
  opacity: .35;
  cursor: not-allowed;
}

/* ==========================================================================
   5. Keypad — CSS Grid 4 cột × 5 hàng
   ========================================================================== */
.keypad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--gap);
}

.key {
  min-height: 58px;
  border: 1px solid var(--key-line);
  border-radius: var(--radius-sm);
  background: var(--key-bg);
  color: var(--text);
  box-shadow: 0 2px 0 rgba(15, 23, 42, .05);
  cursor: pointer;
  font-size: 1.2rem;
  font-weight: 750;
  transition: transform .07s ease, background .14s ease, border-color .14s ease, box-shadow .14s ease;
  -webkit-tap-highlight-color: transparent;
}

.key:hover {
  background: var(--key-hover);
  border-color: var(--line-strong);
}

/* :active = chuột/ngón tay đang nhấn; .is-pressed = JS thêm khi gõ phím vật lý. */
.key:active,
.key.is-pressed {
  transform: translateY(1px) scale(.97);
  box-shadow: none;
}

.key.is-pressed {
  border-color: var(--accent);
}

.key:focus-visible,
.icon-button:focus-visible,
.copy-button:focus-visible,
.text-button:focus-visible,
.history-item:focus-visible,
.learning-panel>summary:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.key-operator {
  border-color: var(--accent-line);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.key-operator:hover {
  background: var(--accent-hover);
}

.key-operator.is-selected {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
}

.key-control {
  background: var(--key-control-bg);
  color: var(--key-control-text);
}

.key-control[data-action="clear"] {
  color: var(--danger);
}

.key-equal {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
  font-size: 1.4rem;
}

.key-equal:hover {
  background: var(--accent-strong);
}

.keyboard-hint {
  margin: 13px 2px 0;
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.9;
}

/* ==========================================================================
   6. History
   ========================================================================== */
.history-card {
  width: min(100%, 430px);
  margin: 0 auto;
  padding: 18px;
}

@media (min-width: 760px) {
  .history-card {
    width: 100%;
  }
}

.text-button {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  padding: 4px 12px;
  color: var(--danger);
  cursor: pointer;
  font-size: .8rem;
  font-weight: 700;
}

.text-button:disabled {
  color: var(--muted);
  opacity: .5;
  cursor: not-allowed;
}

.history-empty {
  margin: 0 0 12px;
  padding: 18px 12px;
  border: 1px dashed var(--line);
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-size: .85rem;
  line-height: 1.6;
  text-align: center;
}

.history-list {
  display: grid;
  gap: 6px;
  max-height: 340px;
  margin: 0 0 12px;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.history-item {
  display: grid;
  width: 100%;
  gap: 2px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-soft);
  color: var(--text);
  cursor: pointer;
  text-align: right;
}

.history-item:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.history-expression {
  color: var(--muted);
  font-size: .8rem;
  overflow-wrap: anywhere;
}

.history-result {
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
  font-weight: 750;
  overflow-wrap: anywhere;
}

/* ==========================================================================
   7. Learning panel
   ========================================================================== */
.learning-panel {
  width: min(100%, 600px);
  margin: 18px auto 0;
  overflow: hidden;
}

.learning-panel>summary {
  cursor: pointer;
  padding: 14px 16px;
  background: var(--surface-soft);
  font-weight: 800;
}

.learning-panel[open]>summary {
  border-bottom: 1px solid var(--line);
}

.state-grid {
  display: grid;
  grid-template-columns: minmax(130px, .8fr) minmax(0, 1.2fr);
  margin: 14px 16px 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.state-grid>* {
  min-width: 0;
  padding: 9px 11px;
  border-bottom: 1px solid var(--line);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: .82rem;
  overflow-wrap: anywhere;
}

.state-grid>*:nth-last-child(-n+2) {
  border-bottom: 0;
}

.state-grid>*:nth-child(odd) {
  border-right: 1px solid var(--line);
}

.state-label {
  background: var(--surface-soft);
  color: var(--text-soft);
  font-weight: 700;
}

.state-grid output {
  background: var(--surface);
  color: var(--text-strong);
}

.panel-note {
  margin: 0;
  padding: 0 16px 15px;
  color: var(--muted);
  font-size: .8rem;
  line-height: 1.45;
}

.history-card .panel-note {
  padding: 0;
}

.concept-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  width: min(100%, 600px);
  margin: 18px auto 0;
}

.concept-strip span {
  padding: 9px 8px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-glass);
  color: var(--text-soft);
  text-align: center;
  font-size: .72rem;
  font-weight: 700;
}

.app-footer {
  padding: 20px 0 0;
  color: var(--muted);
  text-align: center;
  font-size: .78rem;
}

.app-footer p {
  margin-bottom: 0;
}

/* Thông báo nhỏ (copy, xóa lịch sử) — trượt lên từ đáy màn hình. */
.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  margin: 0;
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text-strong);
  color: var(--surface);
  font-size: .85rem;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity .18s ease, transform .18s ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ==========================================================================
   8. Responsive
   ========================================================================== */
@media (max-width: 560px) {
  .app-shell {
    width: min(100% - 20px, 820px);
    padding-top: 22px;
  }

  .app-header {
    align-items: center;
  }

  .version-badge {
    display: none;
  }

  .calculator-card,
  .history-card {
    padding: 14px;
    border-radius: 18px;
  }

  .keypad {
    gap: 8px;
  }

  .key {
    min-height: 54px;
  }

  .concept-strip {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 360px) {
  .app-shell {
    width: calc(100% - 14px);
  }

  .calculator-card,
  .history-card {
    padding: 10px;
  }

  .keypad {
    gap: 6px;
  }

  .key {
    min-height: 50px;
    border-radius: 8px;
  }

  .state-grid {
    grid-template-columns: 1fr;
  }

  .state-grid>*:nth-child(odd) {
    border-right: 0;
    background: var(--surface-soft);
  }

  .state-grid>* {
    border-bottom: 1px solid var(--line) !important;
  }

  .state-grid>*:last-child {
    border-bottom: 0 !important;
  }
}

@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition: none !important;
    animation: none !important;
  }
}
