* { box-sizing: border-box; }

:root {
  --ink: #3d3b45;
  --muted: #777481;
  --paper: rgba(255,255,255,.96);
  --paper-2: #fcfbfd;
  --line: #e8e4eb;
  --accent: #7a5a96;
  --accent-dark: #604078;
  --soft: #f1eaf5;
  --button-text: #ffffff;
  --button-soft-text: #53366d;
  --danger: #b54f5f;
  --success: #47795f;
  --shadow: 0 24px 70px rgba(58, 42, 70, .16);
  --page-bg: linear-gradient(135deg, #eee6f0 0%, #f7eee9 50%, #e8edf4 100%);
}

html[data-theme="dark"] {
  --ink: #f1edf5;
  --muted: #b8b0c2;
  --paper: rgba(30,27,35,.98);
  --paper-2: #25212b;
  --line: #4a4252;
  --accent: #b99ad1;
  --accent-dark: #e0ccef;
  --soft: #37303f;
  --button-text: #1b1520;
  --button-soft-text: #f0e7f6;
  --danger: #f19aaa;
  --success: #9ed1b0;
  --shadow: 0 24px 70px rgba(0,0,0,.34);
  --page-bg: linear-gradient(135deg, #17151b 0%, #211b23 50%, #171a21 100%);
}

html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: 'Poppins', sans-serif;
  color: var(--ink);
  background: var(--page-bg);
  transition: background .25s ease, color .25s ease;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(rgba(255,255,255,.08), rgba(255,255,255,.08));
}

body.modal-open { overflow: hidden; }

button, input, textarea { font: inherit; }
button { border: 0; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .48; transform: none !important; box-shadow: none !important; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 3px solid rgba(185,154,209,.45); outline-offset: 2px; }

.page-shell {
  width: min(920px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 34px 18px 22px;
}

.calculator-card, .history-card {
  width: 100%;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.calculator-card { padding: 38px; }
.history-card { margin-top: 18px; padding: 26px 30px; }

.header-row, .section-heading, .note-heading-row, .history-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.section-heading { flex-wrap: wrap; }
.section-heading > div:first-child { margin-right: auto; }

.card-header { margin-bottom: 28px; }
.eyebrow, .section-kicker, .note-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .18em;
  color: var(--accent);
}

h1 { margin: 7px 0 6px; font-size: clamp(34px, 6vw, 50px); line-height: 1.04; letter-spacing: -.045em; }
h2 { margin: 4px 0 0; font-size: 18px; letter-spacing: -.02em; }
.card-header p { margin: 0; max-width: 620px; color: var(--muted); font-size: 13px; line-height: 1.65; }

.theme-btn {
  width: 44px; height: 44px; flex: 0 0 auto;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--paper-2); color: var(--ink); font-size: 18px;
  transition: transform .18s ease, background .18s ease, border-color .18s ease;
}
.theme-btn:hover { background: var(--soft); border-color: var(--accent); transform: translateY(-1px); }

.secondary-btn, .copy-btn, .theme-btn, .preset-btn, .history-action {
  -webkit-tap-highlight-color: transparent;
}

.secondary-btn {
  min-height: 42px;
  padding: 0 15px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--soft);
  color: var(--button-soft-text);
  font-size: 12px;
  font-weight: 700;
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease, color .18s ease, border-color .18s ease;
}
.secondary-btn:hover { background: var(--line); color: var(--ink); border-color: var(--accent); transform: translateY(-1px); }
.secondary-btn.compact { width: auto; white-space: nowrap; }

.live-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper-2);
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .04em;
}
.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(122,90,150,.12);
}

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.field { display: block; min-width: 0; margin-bottom: 14px; }
.field > span:first-child { display: block; margin-bottom: 8px; font-size: 12px; font-weight: 600; color: var(--ink); }
.field-hint { display: block; margin-top: 6px; color: var(--muted); font-size: 10px; line-height: 1.45; }

.input-wrap { position: relative; }
.input-wrap input {
  width: 100%; height: 58px;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--paper-2); color: var(--ink);
  padding: 0 18px; outline: none;
  font-size: 15px; font-weight: 500;
  transition: border-color .2s ease, box-shadow .2s ease, background .2s ease;
}
.input-wrap:has(.prefix) input { padding-left: 42px; }
.input-wrap:has(.suffix) input { padding-right: 48px; }
.input-wrap input::placeholder { color: var(--muted); }
.input-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(141,112,168,.12); }
.prefix, .suffix { position: absolute; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 13px; font-weight: 700; z-index: 1; }
.prefix { left: 17px; }
.suffix { right: 17px; }

.preset-row { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 9px; }
.preset-btn {
  padding: 7px 11px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--paper-2); color: var(--muted); font-size: 11px; font-weight: 600;
  transition: transform .18s ease, background .18s ease, color .18s ease, border-color .18s ease;
}
.preset-btn:hover, .preset-btn.active { background: var(--soft); color: var(--button-soft-text); border-color: var(--accent); transform: translateY(-1px); }

.error { display: block; min-height: 16px; margin-top: 5px; color: var(--danger); font-size: 11px; line-height: 1.4; }
.invalid input { border-color: var(--danger); box-shadow: 0 0 0 3px rgba(181,79,95,.1); }

.results { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin: 20px 0 26px; }
.result-box { border: 1px solid var(--line); border-radius: 16px; padding: 18px 20px; background: var(--paper-2); }
.result-box span { display: block; color: var(--muted); font-size: 11px; font-weight: 600; margin-bottom: 7px; }
.result-box strong { display: block; font-size: 27px; letter-spacing: -.025em; min-height: 38px; }
.highlighted { background: linear-gradient(135deg, rgba(248,241,250,.95), rgba(255,248,245,.95)); border-color: #e6d9eb; }
html[data-theme="dark"] .highlighted { background: linear-gradient(135deg, #302637, #322923); border-color: #564a60; }
.highlighted strong { color: var(--accent-dark); }

.note-panel { margin-top: 6px; padding: 22px; border-radius: 18px; background: var(--soft); border: 1px dashed var(--line); }
.note-state { color: var(--muted); font-size: 10px; font-weight: 700; text-align: right; }
.note-panel textarea {
  width: 100%; min-height: 220px; margin-top: 16px; resize: vertical;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--paper-2); color: var(--ink); padding: 15px 16px;
  line-height: 1.7; font-size: 13px; outline: none;
}
.note-panel textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(141,112,168,.12); }
.note-panel textarea::placeholder { color: var(--muted); }
.note-actions { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.copy-btn {
  padding: 0 16px; min-height: 44px; border: 1px solid var(--accent);
  background: var(--accent); color: var(--button-text); font-size: 12px; font-weight: 700;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(112,84,138,.18);
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease, color .18s ease, border-color .18s ease;
}
.copy-btn:hover { background: var(--accent-dark); color: var(--button-text); border-color: var(--accent-dark); transform: translateY(-1px); }
.copy-status { min-height: 18px; color: var(--success); font-size: 11px; font-weight: 600; }

.history-heading-row h2 { margin-top: 5px; }
.history-list { display: grid; gap: 9px; margin-top: 18px; }
.history-empty { color: var(--muted); font-size: 12px; padding: 4px 0; }
.history-item {
  display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 12px;
  padding: 12px 14px; border: 1px solid var(--line); border-radius: 13px; background: var(--paper-2);
}
.history-summary { min-width: 0; }
.history-summary strong { display: block; font-size: 13px; }
.history-summary span { color: var(--muted); font-size: 11px; }
.history-action { padding: 6px 8px; background: transparent; color: var(--accent-dark); font-size: 11px; font-weight: 700; }
.history-action:hover { text-decoration: underline; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(20, 16, 24, .58);
  backdrop-filter: blur(5px);
}
.modal-backdrop[hidden] { display: none; }
.confirm-modal {
  width: min(430px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 30px 90px rgba(0,0,0,.3);
  animation: modal-in .16s ease-out;
}
.confirm-icon {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  margin-bottom: 16px;
  border-radius: 50%;
  background: var(--soft);
  color: var(--accent-dark);
  font-size: 18px;
  font-weight: 800;
}
.confirm-copy h2 { margin-top: 6px; font-size: 21px; }
.confirm-copy p { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.6; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 22px; }
.confirm-danger { min-height: 42px; }
@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.toast-root {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 1200;
  display: grid;
  gap: 9px;
  width: min(360px, calc(100vw - 36px));
  pointer-events: none;
}
.toast {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 16px 45px rgba(20,16,24,.22);
  font-size: 12px;
  font-weight: 600;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .18s ease, transform .18s ease;
}
.toast.is-visible { opacity: 1; transform: translateY(0); }
.toast-success { border-color: color-mix(in srgb, var(--success) 35%, var(--line)); }
.toast-error { border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }

footer { display: flex; justify-content: space-between; gap: 12px; padding: 16px 3px 0; color: var(--muted); font-size: 10px; }

@media (max-width: 700px) {
  .page-shell { padding: 16px 10px 22px; }
  .calculator-card { padding: 26px 18px; border-radius: 22px; }
  .history-card { padding: 22px 18px; border-radius: 20px; }
  .field-grid, .results { grid-template-columns: 1fr; gap: 0; }
  .results { gap: 12px; margin: 16px 0 22px; }
  .header-row { align-items: flex-start; }
  .section-heading, .note-heading-row, .history-heading-row { align-items: flex-start; }
  .live-badge { order: 3; }
  .history-item { grid-template-columns: 1fr auto; }
  .history-action { grid-column: 2; }
  .history-summary { grid-column: 1; grid-row: 1 / span 2; }
  .note-state { max-width: 120px; }
  .confirm-actions { flex-direction: column-reverse; }
  .confirm-actions button { width: 100%; }
  .toast-root { right: 10px; bottom: 10px; width: calc(100vw - 20px); }
  footer { flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
  .confirm-modal, .toast { animation: none; transition: none; }
}
