:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: rgba(255, 255, 255, 0.88);
  --surface-strong: #ffffff;
  --surface-muted: #eef1f8;
  --text: #1d2330;
  --text-muted: #6e7787;
  --border: #dfe4ee;
  --accent: #5b63f6;
  --accent-strong: #474fe2;
  --accent-soft: #e8eaff;
  --danger: #d94d63;
  --warning: #b86d13;
  --shadow: 0 18px 55px rgba(37, 46, 77, 0.11);
  --radius-xl: 24px;
  --radius-lg: 18px;
  --radius-md: 13px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1118;
  --surface: rgba(20, 26, 36, 0.9);
  --surface-strong: #171d27;
  --surface-muted: #202735;
  --text: #f3f5f8;
  --text-muted: #9da7b7;
  --border: #30394a;
  --accent: #858cff;
  --accent-strong: #9ba1ff;
  --accent-soft: #2a315a;
  --danger: #ff7688;
  --warning: #f2ad53;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.33);
}

* { box-sizing: border-box; }

html {
  min-height: 100%;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  color: var(--text);
  background:
    radial-gradient(circle at 12% -10%, rgba(91, 99, 246, 0.18), transparent 34rem),
    radial-gradient(circle at 94% 16%, rgba(99, 187, 255, 0.12), transparent 28rem),
    var(--bg);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei UI", sans-serif;
}

button, input, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app-shell {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: calc(18px + var(--safe-top)) 18px calc(24px + var(--safe-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-icon {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  box-shadow: 0 9px 24px rgba(75, 83, 232, 0.3);
}

h1, h2, p { margin: 0; }
.brand-block h1 { font-size: 22px; letter-spacing: -0.03em; }
.brand-block p { margin-top: 3px; color: var(--text-muted); font-size: 13px; }
.topbar-actions, .result-actions { display: flex; gap: 8px; }

.icon-button,
.text-button,
.secondary-button,
.primary-button,
.key,
.tab-button {
  border: 0;
  cursor: pointer;
  color: var(--text);
}

.icon-button {
  width: 42px;
  height: 42px;
  border-radius: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 5px 18px rgba(39, 48, 78, 0.07);
  font-size: 20px;
}

.icon-button:active,
.key:active,
.tab-button:active,
.primary-button:active,
.secondary-button:active { transform: translateY(1px) scale(0.985); }

.main-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(310px, 0.7fr);
  gap: 18px;
  align-items: start;
}

.workspace-card,
.result-card {
  border: 1px solid var(--border);
  background: var(--surface);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow);
}

.workspace-card { padding: 20px; }
.result-card { padding: 20px; min-height: 330px; position: sticky; top: 18px; }

.operation-row {
  display: grid;
  grid-template-columns: auto minmax(170px, 0.7fr) minmax(160px, 1fr);
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}

.field-label,
.eyebrow {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.select-control,
.text-control {
  width: 100%;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  background: var(--surface-strong);
  padding: 0 12px;
  outline: none;
}

.select-control:focus,
.text-control:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-heading h2 { margin-top: 2px; font-size: 20px; letter-spacing: -0.025em; }

.text-button {
  background: transparent;
  color: var(--accent);
  padding: 7px 4px;
  font-weight: 700;
  font-size: 13px;
}
.text-button:disabled { opacity: 0.42; cursor: default; }

.formula-field,
.result-field {
  display: block;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text);
  background: var(--surface-strong);
  --caret-color: var(--accent);
  --selection-background-color: var(--accent-soft);
  --contains-highlight-background-color: var(--accent-soft);
  --smart-fence-color: var(--accent);
  --placeholder-color: var(--text-muted);
}

.formula-field {
  min-height: 120px;
  max-height: 245px;
  overflow: auto;
  padding: 23px 18px;
  font-size: clamp(26px, 5vw, 36px);
  line-height: 1.55;
}

.formula-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent); }
.formula-field::part(virtual-keyboard-toggle),
.formula-field::part(menu-toggle),
.result-field::part(virtual-keyboard-toggle),
.result-field::part(menu-toggle) { display: none; }

.latex-line {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 9px;
  align-items: center;
  min-height: 36px;
  margin: 8px 4px 15px;
  color: var(--text-muted);
  font-size: 12px;
}

.latex-line code {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
}

.keyboard {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--surface-muted) 78%, transparent);
  overflow: hidden;
}

.keyboard-toolbar {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 7px;
  padding: 9px;
  border-bottom: 1px solid var(--border);
}

.keyboard-toolbar .wide { grid-column: span 1; font-size: 11px; }

.keyboard-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px 9px 0;
}

.tab-button {
  min-height: 37px;
  border-radius: 10px 10px 5px 5px;
  background: transparent;
  color: var(--text-muted);
  font-weight: 700;
}

.tab-button.active { color: var(--accent); background: var(--surface-strong); }

.keyboard-panel {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  padding: 9px;
  min-height: 170px;
}

.key {
  min-width: 0;
  min-height: 45px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface-strong);
  box-shadow: 0 3px 9px rgba(38, 48, 77, 0.06);
  font-weight: 650;
}

.nav-key { min-height: 37px; font-size: 13px; }
.danger-key { color: var(--danger); }
.key.function-key { color: var(--accent); }
.key.structure-key { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 25%, var(--border)); }

.action-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-top: 16px;
}

.switch-label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 13px;
}
.switch-label input { accent-color: var(--accent); width: 17px; height: 17px; }

.primary-button,
.secondary-button {
  min-height: 46px;
  border-radius: 13px;
  padding: 0 20px;
  font-weight: 800;
}

.primary-button {
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  box-shadow: 0 10px 25px color-mix(in srgb, var(--accent) 30%, transparent);
}
.primary-button:disabled { opacity: 0.58; cursor: wait; }
.primary-button.full { width: 100%; }
.secondary-button { width: 100%; background: var(--surface-muted); color: var(--text); }

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-left: 8px;
  vertical-align: -3px;
  border: 2px solid rgba(255,255,255,.45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result-field {
  min-height: 165px;
  padding: 28px 18px;
  font-size: clamp(25px, 4vw, 34px);
  overflow: auto;
}

.markdown-result {
  overflow: auto;
  margin: 13px 0 0;
  padding: 13px;
  border-radius: 12px;
  background: var(--surface-muted);
  color: var(--text-muted);
  font-size: 12px;
}

.notice {
  margin-bottom: 12px;
  padding: 13px 14px;
  border-radius: 13px;
  font-size: 13px;
}
.notice p { margin-top: 5px; line-height: 1.55; }
.notice.warning { color: var(--warning); background: color-mix(in srgb, var(--warning) 11%, transparent); }
.notice.error { color: var(--danger); background: color-mix(in srgb, var(--danger) 11%, transparent); white-space: pre-wrap; }

.app-footer {
  display: flex;
  justify-content: space-between;
  gap: 15px;
  padding: 14px 4px 0;
  color: var(--text-muted);
  font-size: 11px;
}

.drawer {
  position: fixed;
  inset: 0;
  z-index: 80;
  pointer-events: none;
  visibility: hidden;
}
.drawer.open { pointer-events: auto; visibility: visible; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(5, 8, 14, 0.42); opacity: 0; transition: opacity .2s ease; }
.drawer.open .drawer-backdrop { opacity: 1; }
.drawer-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: min(420px, 92vw);
  height: 100%;
  padding: calc(20px + var(--safe-top)) 18px calc(20px + var(--safe-bottom));
  border-left: 1px solid var(--border);
  background: var(--surface-strong);
  box-shadow: -20px 0 55px rgba(0,0,0,.18);
  transform: translateX(102%);
  transition: transform .24s ease;
  display: flex;
  flex-direction: column;
}
.drawer.open .drawer-panel { transform: translateX(0); }
.drawer-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.history-list { flex: 1; min-height: 0; overflow: auto; display: grid; align-content: start; gap: 10px; padding-right: 2px; }
.history-empty { padding: 30px 10px; text-align: center; color: var(--text-muted); }
.history-item { border: 1px solid var(--border); border-radius: 14px; padding: 12px; background: var(--surface); cursor: pointer; }
.history-item:hover { border-color: var(--accent); }
.history-meta { display: flex; justify-content: space-between; gap: 9px; color: var(--text-muted); font-size: 11px; }
.history-expression, .history-result { margin-top: 7px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: "Cascadia Code", monospace; font-size: 12px; }
.history-result { color: var(--accent); }

.install-dialog {
  width: min(440px, calc(100vw - 28px));
  border: 0;
  padding: 0;
  border-radius: 24px;
  color: var(--text);
  background: var(--surface-strong);
  box-shadow: 0 28px 100px rgba(0,0,0,.3);
}
.install-dialog::backdrop { background: rgba(5,8,14,.53); backdrop-filter: blur(5px); }
.install-card { position: relative; padding: 28px; text-align: center; }
.dialog-close { position: absolute; right: 13px; top: 13px; width: 36px; height: 36px; border: 0; border-radius: 50%; background: var(--surface-muted); color: var(--text); font-size: 21px; }
.install-icon { width: 76px; height: 76px; border-radius: 21px; margin-bottom: 13px; }
.install-card h2 { margin-bottom: 18px; }
.install-steps { display: grid; gap: 12px; padding: 0; list-style: none; text-align: left; }
.install-steps li { display: grid; grid-template-columns: 30px 1fr; gap: 10px; align-items: center; line-height: 1.55; }
.step-index { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; color: #fff; background: var(--accent); font-weight: 800; }
.install-note { margin: 18px 0; color: var(--text-muted); font-size: 13px; line-height: 1.6; }

.toast {
  position: fixed;
  z-index: 120;
  left: 50%;
  bottom: calc(18px + var(--safe-bottom));
  transform: translate(-50%, 20px);
  min-width: 150px;
  max-width: calc(100vw - 30px);
  padding: 11px 15px;
  border-radius: 999px;
  color: #fff;
  background: rgba(21, 27, 39, .92);
  box-shadow: 0 10px 35px rgba(0,0,0,.23);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
  text-align: center;
  font-size: 13px;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 850px) {
  .app-shell { padding-left: 12px; padding-right: 12px; }
  .main-layout { grid-template-columns: 1fr; }
  .result-card { position: static; min-height: 0; }
  .keyboard-toolbar { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .keyboard-panel { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .app-shell { padding-top: calc(12px + var(--safe-top)); }
  .brand-block p { display: none; }
  .brand-icon { width: 42px; height: 42px; border-radius: 12px; }
  .topbar { margin-bottom: 12px; }
  .topbar-actions { gap: 5px; }
  .icon-button { width: 39px; height: 39px; border-radius: 12px; }
  .workspace-card, .result-card { padding: 14px; border-radius: 19px; }
  .operation-row { grid-template-columns: auto 1fr; }
  .variables-control { grid-column: 1 / -1; }
  .formula-field { min-height: 105px; padding: 18px 12px; font-size: 28px; }
  .keyboard-toolbar { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .keyboard-panel { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 7px; }
  .key { min-height: 44px; }
  .action-row { align-items: stretch; flex-direction: column; }
  .primary-button { width: 100%; }
  .result-heading { align-items: center; }
  .result-actions { flex-direction: column; gap: 0; align-items: end; }
  .app-footer { flex-direction: column; gap: 4px; }
}

@media (display-mode: standalone) {
  .app-shell { padding-top: calc(12px + var(--safe-top)); }
  #installButton { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
