/* Dark theme variables */
:root {
  --bg: #0b0f14;
  --surface: #0f141b;
  --surface-2: #111822;
  --border: #1e2733;
  --text: #e6eef6;
  --muted: #9fb1c3;
  --accent: #6ecbff;
  --accent-2: #48a8ff;
  --danger: #ff6b6b;
  --warning: #ffd166;
  --success: #5ad49b;
  --shadow: 0 6px 20px rgba(0,0,0,0.35);
  --radius: 14px;
}

:root.theme-light {
  --bg: #f7fbff;
  --surface: #ffffff;
  --surface-2: #f5f7fb;
  --border: #d6e0ea;
  --text: #0c2235;
  --muted: #5a768f;
  --accent: #246bff;
  --accent-2: #3e8cff;
  --danger: #d22f2f;
  --warning: #b88700;
  --success: #1e9a5a;
  --shadow: 0 6px 20px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  background: radial-gradient(1200px 800px at 20% -10%, #0d1724 0%, var(--bg) 55%),
              radial-gradient(900px 500px at 120% 0%, #12283f 0%, transparent 50%),
              var(--bg);
  background-repeat: no-repeat, no-repeat, no-repeat;
  background-attachment: fixed, fixed, fixed;
  background-size: 1600px 1100px, 1200px 700px, auto;
  color: var(--text);
  line-height: 1.5;
}

:root.theme-light body {
  background: var(--bg);
}

.app-header {
  position: sticky; top: 0; z-index: 10;
  backdrop-filter: blur(8px);
  background: linear-gradient(180deg, rgba(10,14,19,0.85) 0%, rgba(10,14,19,0.65) 100%);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 12px;
  padding: 16px clamp(16px, 4vw, 32px);
}
.brand { justify-content: space-between; }
.header-actions { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 1.25rem; letter-spacing: 0.2px; }

:root.theme-light .app-header { backdrop-filter: none; background: var(--surface); }
.brand {
  display: flex; align-items: center; gap: 12px;
  padding: 16px clamp(16px, 4vw, 32px);
}
.brand { justify-content: space-between; }
.header-actions { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 1.25rem; letter-spacing: 0.2px; }

.container { max-width: 1100px; margin: 32px auto; padding: 0 clamp(16px, 4vw, 32px); }

.card {
  background: linear-gradient(180deg, var(--surface) 0%, var(--surface-2) 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
.card-head h2 { margin: 0; font-size: 1.1rem; }
.muted { color: var(--muted); }

.grid { display: grid; gap: 16px; }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 860px) { .grid-3 { grid-template-columns: 1fr; } }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.field { display: grid; gap: 8px; }
label { color: var(--muted); font-size: 0.9rem; }
input[type="text"], textarea {
  width: 100%;
  background: #0c121a;
  color: var(--text);
  border: 1px solid var(--border);
  outline: none;
  padding: 12px 12px;
  border-radius: 10px;
  transition: border-color .2s ease, box-shadow .2s ease, background .2s ease;
}
input::placeholder, textarea::placeholder { color: #7892aa; }
input:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 6px rgba(78, 186, 255, 0.12);
  background: #0b1219;
}

:root.theme-light input[type="text"],
:root.theme-light textarea { background: #ffffff; }
:root.theme-light input:focus,
:root.theme-light textarea:focus { box-shadow: none; }
.hint { color: #7ea5c4; }
.field-msg { min-height: 18px; font-size: 0.85rem; color: var(--muted); }
.field-msg.error { color: var(--danger); }
.field-msg.success { color: var(--success); }

/* Valid/invalid input colors */
.is-valid { border-color: rgba(90, 212, 155, 0.9) !important; box-shadow: 0 0 0 6px rgba(90, 212, 155, 0.12) !important; }
.is-invalid { border-color: rgba(255, 107, 107, 0.95) !important; box-shadow: 0 0 0 6px rgba(255, 107, 107, 0.12) !important; }

.note.info { border-left: 4px solid var(--accent); }

.app-footer {
  max-width: 1100px; margin: 28px auto 40px; padding: 0 clamp(16px, 4vw, 32px);
  display: flex; align-items: center; justify-content: space-between; color: var(--muted);
}

.btn { display: inline-flex; align-items: center; gap: 8px; border-radius: 10px; text-decoration: none; cursor: pointer; }
.btn.ghost { color: var(--text); padding: 10px 12px; border: 1px solid var(--border); background: transparent; }
.btn.ghost:hover { border-color: var(--accent); box-shadow: 0 0 0 6px rgba(78, 186, 255, 0.1); }
.btn.icon-only { padding: 8px 10px; width: 36px; height: 36px; display: inline-grid; place-items: center; }
.header-actions #latex-toggle { width: 84px; justify-content: center; white-space: nowrap; min-height: 40px; }
/* Normalize KaTeX sizing and alignment inside the LaTeX toggle to avoid jumps */
#latex-toggle .katex { font-size: 1em; vertical-align: middle; line-height: 1; }
.auto-btn { min-width: 210px; justify-content: center; }
/* Consistent width for primary actions across cards */
.action-btn { width: 210px; justify-content: center; }
/* Keep fixed width inside flex rows */
.row .action-btn { flex: 0 0 210px; }
@media (max-width: 480px) { .auto-btn { min-width: 0; width: 100%; } }
@media (max-width: 480px) { .action-btn { width: 100%; } }
@media (max-width: 480px) { .row .action-btn { flex: 1 1 100%; } }

/* Theme toggle icons */
.icon-sun, .icon-moon { width: 20px; height: 20px; display: none; }
.icon-sun *, .icon-moon * { fill: none; stroke: var(--text); stroke-linecap: round; stroke-linejoin: round; }
.btn.icon-only .icon-sun, .btn.icon-only .icon-moon { transform: translateX(-3px); }
/* Show the icon for the CURRENT theme (moon in dark, sun in light) */
:root.theme-light .icon-sun { display: block; }
:root.theme-light .icon-moon { display: none; }
:root:not(.theme-light) .icon-sun { display: none; }
:root:not(.theme-light) .icon-moon { display: block; }

/* Utility */
.hidden { display: none !important; }
.row { display: flex; gap: 12px; align-items: center; }
.row > input[type="text"] { flex: 1 1 auto; }
.primes-row { align-items: stretch; }
.primes-row > input[type="text"] { flex: 1 1 0; min-width: 0; }
.primes-row > input[type="text"]:first-child { margin-right: 0; }
.primes-row .auto-btn { flex: 0 0 auto; }
@media (max-width: 640px) {
  .primes-row { flex-wrap: wrap; }
  .primes-row > input[type="text"] { flex: 1 1 100%; }
  .primes-row .auto-btn { width: 100%; }
}
.badge { padding: 2px 8px; border-radius: 999px; background: #0d1a26; border: 1px solid var(--border); color: var(--muted); font-size: 0.75rem; }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: grid; place-items: center; padding: 24px; }
.modal { width: min(640px, 96vw); background: linear-gradient(180deg, var(--surface) 0%, var(--surface-2) 100%); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); max-height: 80vh; overflow: hidden; display: flex; flex-direction: column; }
/* Make the Generate Keys (stepper) modal ~10% wider */
#stepper-overlay .modal { width: min(704px, 96vw); }
.modal-head, .modal-foot { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--border); flex: 0 0 auto; }
.modal-foot { border-top: 1px solid var(--border); border-bottom: none; }
.modal-head h3 { margin: 0; font-weight: 600; font-size: 1.05rem; }
.modal-body { padding: 18px; overflow: auto; flex: 1 1 auto; }

@media (max-width: 640px) {
  .modal { width: min(96vw, 640px); max-height: 88vh; }
}

/* Sign output */
.sign-ascii { margin-top: 10px; color: var(--muted); }
.sign-ascii code { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; color: var(--text); }
.sign-ascii a { color: var(--accent); text-decoration: none; border-bottom: 1px dashed rgba(78,186,255,0.4); }
.sign-ascii a:hover { color: var(--accent-2); border-bottom-color: rgba(62,140,255,0.6); }
/* Math lines */
.math-line { margin-top: 10px; }
.math-exp { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; color: var(--text); }
.math-exp sup { font-size: 0.8em; vertical-align: super; }

/* Scrollable long text fields */
.scroll-field {
  width: 100%;
  background: #0c121a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  line-height: 1.4;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  resize: none;
}
:root.theme-light .scroll-field { background: #ffffff; }

/* ASCII table */
.ascii-table-wrapper { max-height: 55vh; overflow: auto; border: 1px solid var(--border); border-radius: 10px; }
.ascii-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.ascii-table th, .ascii-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; }
.ascii-table th:first-child, .ascii-table td:first-child { width: 90px; }
.ascii-table thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.ascii-table tbody tr:nth-child(odd) { background: rgba(255,255,255,0.02); }
.ascii-char { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
.ascii-printable { color: var(--text); }
.ascii-control { color: var(--muted); }
