/**
 * SumDay Main Styles (minimalist, iOS Safari-friendly)
 *
 * Changes:
 * - Removed animated gradients + heavy glass effects
 * - Wordle-like header (flat, bordered)
 * - Icon buttons are plain (no capsule backgrounds)
 * - Bottom operator panel docks flush to Safari URL bar
 * - Uses dynamic viewport units + safe-area insets on iOS
 */

/* Reset and Base Styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  /* Typography */
  --font-family: "Courier New", Courier, monospace;
  --digit-size: 1.45rem;
  --operator-size: 1.35rem;

  /* Spacing */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
  --spacing-xl: 2rem;

  /* Layout */
  --max-width: 800px;
  --dock-height: 190px; /* reserve space above the fixed operator dock */
  --meta-footer-height: 44px;

  /* Light theme (Wordle-ish) */
  --bg-color: #ffffff;
  --surface-color: #ffffff;
  --text-color: #111827;
  --text-secondary: #6b7280;
  --border-color: #d1d5db;
  --border-color-strong: #cbd5e1;

  --primary-color: #4f46e5; /* indigo */
  --success-color: #16a34a;
  --error-color: #dc2626;
  --warning-color: #d97706;

  --success-bg: #dcfce7;
  --error-bg: #fee2e2;
  --warning-bg: #fef3c7;
  --info-bg: #eef2ff;

  --key-bg: #e5e7eb;
  --key-bg-pressed: #d1d5db;

  /* iOS safe areas */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Dark mode variables */
[data-theme="dark"] {
  --bg-color: #121213;
  --surface-color: #121213;
  --text-color: #f9fafb;
  --text-secondary: #cbd5e1;
  --border-color: #3a3a3c;
  --border-color-strong: #4b5563;

  --primary-color: #818cf8;
  --success-color: #22c55e;
  --error-color: #ef4444;
  --warning-color: #f59e0b;

  --success-bg: #052e16;
  --error-bg: #450a0a;
  --warning-bg: #451a03;
  --info-bg: #1f1b3a;

  --key-bg: #3a3a3c;
  --key-bg-pressed: #4b5563;
}

/* iOS Safari rendering stability */
html,
body {
  height: 100%;
  background: var(--bg-color);
}

body {
  font-family: var(--font-family);
  color: var(--text-color);

  /* Dynamic viewport fixes iOS Safari chrome resizing */
  min-height: 100dvh;
  min-height: 100svh;

  /* Prevent iOS rubber-band from showing white */
  overscroll-behavior: none;

  /* No centering, the app should fill the page */
  display: block;
}

/* Main container becomes the app "page" */
.container {
  width: 100%;
  max-width: var(--max-width);
  margin: 0 auto;

  background: var(--surface-color);

  /* Fill visible viewport, then content scrolls internally */
  min-height: 100dvh;
  min-height: 100svh;

  display: flex;
  flex-direction: column;
}

/* Header (minimalist like Wordle) */
.header {
  background: var(--surface-color);
  color: var(--text-color);

  padding: var(--spacing-md) var(--spacing-lg);
  padding-top: calc(var(--spacing-md) + var(--safe-top));

  border-bottom: 1px solid var(--border-color);
}

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--spacing-md);
}

.header-top {
  margin-bottom: var(--spacing-xs);
}

.header-bottom {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.title {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: 0.5px;
}

.logo-link {
  color: inherit;
  text-decoration: none;
}

.logo-link:focus-visible {
  outline: 2px solid var(--primary-color);
  outline-offset: 4px;
  border-radius: 6px;
}

.date-display {
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  color: var(--primary-color);
}

.date-button {
  font-family: var(--font-family);
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
}

.header-layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--spacing-md);
  align-items: start;
}

.header-content {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
}


.logo-mark {
  width: 58px;
  height: 58px;
  aspect-ratio: 1 / 1;
  align-self: center;
  border-radius: 8px;
  background: var(--text-color);
  color: var(--surface-color);
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  place-items: center;
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1;
  min-width: 58px;
  min-height: 58px;
}

[data-theme="dark"] .logo-mark {
  background: var(--surface-color);
  color: var(--text-color);
  border: 1px solid var(--border-color);
}

.number-display-header {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  font-size: 0.95rem;
  font-weight: 700;
}

.number-value-header {
  font-size: 1rem;
  font-weight: 800;
  font-family: var(--font-family);
  letter-spacing: 3px;
  color: var(--text-color);
}

/* Header icons (no capsules) */
.header-icons-left {
  display: flex;
  gap: var(--spacing-sm);
  align-items: center;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 6px;
  border-radius: 8px; /* subtle, not a capsule */
  cursor: pointer;

  display: flex;
  align-items: center;
  justify-content: center;

  width: 32px;
  height: 32px;

  transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.icon-btn:hover {
  background: rgba(0, 0, 0, 0.04);
  color: var(--text-color);
}

[data-theme="dark"] .icon-btn:hover {
  background: rgba(255, 255, 255, 0.06);
}

.icon-btn:active {
  transform: scale(0.96);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

/* Game Container */
.game-container {
  padding: var(--spacing-lg);
  padding-bottom: calc(var(--dock-height) + var(--meta-footer-height) + var(--safe-bottom));
  flex: 1;
}

/* Message Display Area */
.message-area {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--spacing-md);
}

.message {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: 10px;

  font-weight: 600;
  text-align: center;

  border: 1px solid var(--border-color);
  background: var(--info-bg);
  color: var(--text-color);

  max-height: 90px;
  overflow: hidden;

  font-size: 0.95rem;
  line-height: 1.35;
}

.message.success {
  background: var(--success-bg);
  border-color: rgba(22, 163, 74, 0.35);
  color: var(--success-color);
}

.message.error {
  background: var(--error-bg);
  border-color: rgba(220, 38, 38, 0.35);
  color: var(--error-color);
}

.message.warning {
  background: var(--warning-bg);
  border-color: rgba(217, 119, 6, 0.35);
  color: var(--warning-color);
}

.message.info {
  background: var(--info-bg);
  border-color: rgba(79, 70, 229, 0.25);
  color: var(--primary-color);
}

/* Equations Container */
.equations-container {
  display: flex;
  flex-direction: column;

  /* tighter vertical spacing */
  gap: 0;
  margin-bottom: 10px;

  /* do NOT scroll */
  max-height: none;
  overflow: hidden;

  padding: 0;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  font-weight: 700;
}

.meta-label {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.65rem;
}

.meta-value {
  font-size: 0.95rem;
  color: var(--text-color);
  font-variant-numeric: tabular-nums;
}

/* Equation Row (minimal) */
.equation-row {
  display: flex;
  align-items: center;
  justify-content: center;

  /* tighter */
  gap: 2px;
  padding: 20px 2px;

  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border-color);

  border-radius: 0;
  transition: none;
}

/* remove last divider line */
.equation-row:last-child {
  border-bottom: none;
}

.equation-row.active {
  border-color: rgba(79, 70, 229, 0.65);
  transform: scale(1.01);
}

.equation-row.completed {
  border-color: rgba(22, 163, 74, 0.65);
}

.equation-row.completed .digit {
  color: var(--text-secondary);
  opacity: 0.55;
}

.equation-row.locked {
  opacity: 0.35;
  pointer-events: none;
}

/* Digits */
.digit {
  font-size: var(--digit-size);
  font-weight: 800;
  color: var(--text-color);
  font-family: var(--font-family);

  /* narrower so helper mode fits */
  min-width: 1.4rem;
  text-align: center;
}

/* Operator Inputs */
.operator-input {
  width: 1.6rem;
  height: 2.0rem;

  font-size: var(--operator-size);
  font-weight: 900;
  text-align: center;

  background: transparent;
  border: none;
  border-bottom: 2px solid rgba(148, 163, 184, 0.9); /* light grey underscore */
  border-radius: 0;

  color: var(--primary-color);
  font-family: var(--font-family);
  font-variant-numeric: tabular-nums;

  caret-color: transparent;
  user-select: none;
  cursor: pointer;

  transition: border-color 0.12s ease, transform 0.12s ease;
}

.operator-input:hover:not(:disabled) {
  border-bottom-color: rgba(79, 70, 229, 0.75);
}

.operator-input:focus {
  outline: none;
  border-bottom-color: rgba(79, 70, 229, 0.95);
  transform: translateY(-1px);
}

.operator-input:disabled {
  border-bottom-color: transparent;
  color: var(--success-color);
  font-family: var(--font-family);
}

/* Hint reveal styling */
.operator-input.hint-revealed {
  color: var(--text-color);
  font-weight: 900;
  border-color: var(--border-color-strong);
  background: rgba(0, 0, 0, 0.03);
}

[data-theme="dark"] .operator-input.hint-revealed {
  background: rgba(255, 255, 255, 0.06);
}

/* Progress */
.progress-container {
  text-align: center;
  margin-bottom: var(--spacing-xl);
}

.progress {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.timer-display {
  margin-top: var(--spacing-xs);
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.timer-display .timer {
  font-family: "Courier New", monospace;
}

/* Controls */
.controls,
.timer-controls {
  display: flex;
  gap: var(--spacing-md);
  justify-content: center;
  flex-wrap: wrap;
  align-items: center;
}

.btn {
  padding: 0.75rem 1.25rem;
  font-size: 1rem;
  font-weight: 700;
  border: 1px solid var(--border-color);
  border-radius: 12px;

  background: var(--surface-color);
  color: var(--text-color);

  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
  font-family: var(--font-family);
}

.btn:hover {
  transform: translateY(-1px);
  background: rgba(0, 0, 0, 0.03);
}

[data-theme="dark"] .btn:hover {
  background: rgba(255, 255, 255, 0.06);
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  border-color: rgba(79, 70, 229, 0.6);
  color: var(--primary-color);
}

.btn-secondary {
  color: var(--text-secondary);
}

/* Footer */
.footer {
  border-top: 1px solid var(--border-color);
  padding: var(--spacing-lg);
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

/* Bottom Operator Dock (fixed, flush to Safari URL bar) */
.operator-buttons {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--meta-footer-height);

  width: 100%;
  max-width: none;
  transform: none;

  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  align-items: center;

  background: var(--surface-color);
  border-top: 1px solid var(--border-color);

  padding: var(--spacing-md);

  z-index: 100;
}

.operator-buttons.is-hidden {
  display: none;
}

.post-game-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--meta-footer-height);

  width: 100%;
  max-width: none;

  display: flex;
  justify-content: center;

  background: var(--surface-color);
  border-top: 1px solid var(--border-color);

  padding: var(--spacing-md);

  z-index: 100;
}

.post-game-panel.is-hidden {
  display: none;
}

.post-game-actions {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  width: 100%;
  max-width: var(--max-width);
}

.post-game-actions .btn {
  width: 100%;
}

.meta-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 101;

  display: flex;
  justify-content: center;

  padding: var(--spacing-sm) var(--spacing-lg) calc(var(--spacing-sm) + var(--safe-bottom));
  width: 100%;
  min-height: var(--meta-footer-height);

  background: var(--surface-color);
  border-top: 1px solid var(--border-color);

  font-size: 0.85rem;
  color: var(--text-secondary);
}

.meta-footer-inner {
  width: 100%;
  max-width: var(--max-width);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-md);
}

/* Operator rows */
.operator-row {
  display: flex;
  gap: var(--spacing-sm);
  justify-content: center;
  align-items: center;

  width: 100%;
  max-width: var(--max-width);
}

.action-buttons-column {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  width: 100%;
  max-width: var(--max-width);
}

.operator-btn {
  flex: 1;
  height: 64px;
  font-size: 2rem;
  font-weight: 800;

  border: 1px solid var(--border-color-strong);
  border-radius: 14px;

  background: var(--key-bg);
  color: var(--text-color);

  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
  font-family: var(--font-family);
  font-variant-numeric: tabular-nums;
}

.operator-btn:hover {
  transform: translateY(-1px);
}

.operator-btn:active {
  transform: translateY(0);
  background: var(--key-bg-pressed);
}

.operator-action-btn {
  width: 100%;
  padding: 0.85rem 1rem;
  font-size: 1rem;
  font-weight: 700;

  border: 1px solid var(--border-color-strong);
  border-radius: 14px;

  background: var(--surface-color);
  color: var(--text-secondary);

  cursor: pointer;
  font-family: var(--font-family);
  font-variant-numeric: tabular-nums;
}

.operator-action-btn:hover {
  background: rgba(0, 0, 0, 0.03);
}

[data-theme="dark"] .operator-action-btn:hover {
  background: rgba(255, 255, 255, 0.06);
}

/* Hint cooldown bar (kept, but toned down) */
#hint-btn-mobile {
  position: relative;
  overflow: hidden;
}

#hint-btn-mobile .hint-cooldown {
  position: absolute;
  inset: 0;
  background: rgba(79, 70, 229, 0.12);
  width: 0%;
  transition: width 15s linear;
  z-index: 0;
}

#hint-btn-mobile:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Desktop hint bar */
#hint-btn {
  position: relative;
  overflow: hidden;
}

.hint-cooldown {
  position: absolute;
  inset: 0;
  background: rgba(79, 70, 229, 0.12);
  width: 0%;
  transition: width 15s linear;
  z-index: 0;
}

.hint-cooldown.active {
  width: 100%;
}

#hint-btn * {
  position: relative;
  z-index: 2;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  inset: 0;

  background: rgba(0, 0, 0, 0.45);
  justify-content: center;
  align-items: center;
  padding: var(--spacing-md);
}

.modal-content {
  background: var(--surface-color);
  color: var(--text-color);

  border: 1px solid var(--border-color);
  border-radius: 16px;

  padding: var(--spacing-xl);
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}

.modal-content h2 {
  margin-bottom: var(--spacing-md);
  font-size: 1.4rem;
}

.modal-content h3 {
  margin: var(--spacing-lg) 0 var(--spacing-sm);
  font-size: 1.1rem;
}

.modal-content p {
  margin-bottom: var(--spacing-md);
  line-height: 1.5;
}

.modal-content ul {
  padding-left: 1.25rem;
  margin: var(--spacing-sm) 0 var(--spacing-md);
  line-height: 1.5;
}

.modal-content li + li {
  margin-top: var(--spacing-xs);
}

.close-btn {
  position: absolute;
  top: 10px;
  right: 10px;

  background: transparent;
  border: none;

  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text-secondary);

  cursor: pointer;
  line-height: 1;

  width: 34px;
  height: 34px;

  display: flex;
  align-items: center;
  justify-content: center;

  border-radius: 10px;
}

.close-btn:hover {
  background: rgba(0, 0, 0, 0.04);
  color: var(--text-color);
}

[data-theme="dark"] .close-btn:hover {
  background: rgba(255, 255, 255, 0.06);
}

.settings-section {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-md);
}

.setting-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-md);
  padding: var(--spacing-md);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.02);
}

[data-theme="dark"] .setting-item {
  background: rgba(255, 255, 255, 0.03);
}

.setting-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.setting-label {
  font-weight: 700;
}

.setting-description {
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 52px;
  height: 28px;
  flex: 0 0 auto;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border-color);
  border-radius: 999px;
  transition: background 0.2s ease;
  display: flex;
  align-items: center;
}

.toggle-slider::before {
  content: "";
  position: absolute;
  height: 22px;
  width: 22px;
  left: 3px;
  top: 50%;
  background: var(--surface-color);
  border-radius: 50%;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  transition: transform 0.2s ease;
  transform: translateY(-50%);
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--primary-color);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translate(24px, -50%);
}

.toggle-switch input:focus-visible + .toggle-slider {
  outline: 2px solid rgba(79, 70, 229, 0.6);
  outline-offset: 2px;
}

.example-equations {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

.example-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.example-digit,
.example-operator {
  font-size: var(--digit-size);
  font-weight: 800;
}

.share-note {
  margin: 0 0 var(--spacing-sm);
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.share-buttons {
  display: grid;
  gap: var(--spacing-sm);
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.share-buttons .btn {
  width: 100%;
}

.date-picker-container {
  display: flex;
  gap: var(--spacing-sm);
  flex-direction: column;
  align-items: stretch;
  width: 100%;
}

.date-picker {
  padding: 0.75rem 1.25rem;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  color: var(--text-color);
  width: 100%;
  min-height: 48px;
  font-size: 1rem;
  font-weight: 700;
  font-family: var(--font-family);
  text-align: center;
  appearance: none;
  -webkit-appearance: none;
  display: block;
  align-self: center;
}

.date-picker-container .btn {
  width: 100%;
}

/* Helper Mode */
.helper-value-left,
.helper-value-right,
.helper-equals {
  display: none;
  padding: 0 2px;
  min-height: 2rem;
}

.helper-value-left,
.helper-value-right {
  min-width: 1.4rem;
  width: auto;
  text-align: center;
  font-size: var(--digit-size);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.helper-mode-active .helper-value-left,
.helper-mode-active .helper-value-right,
.helper-mode-active .helper-equals {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.helper-equals {
  color: var(--text-secondary);
  width: 1.6rem;
  text-align: center;
  font-size: var(--operator-size);
  font-weight: 800;
}

.equation-row .helper-value-left,
.equation-row .helper-value-right,
.equation-row .helper-equals {
  color: var(--text-secondary);
}

.equation-row.active .helper-value-left,
.equation-row.active .helper-value-right,
.equation-row.active .helper-equals {
  color: var(--primary-color);
}

.equation-row.completed .helper-value-left,
.equation-row.completed .helper-value-right,
.equation-row.completed .helper-equals {
  color: var(--success-color);
}

/* Responsive */
@media (max-width: 768px) {
  .container {
    max-width: 100%;
  }

  .game-container {
    padding: var(--spacing-lg);
    padding-bottom: calc(var(--dock-height) + var(--safe-bottom));
  }

  .equation-row {
    gap: 2px;
    padding: 2px 2px;
  }

  .puzzle-meta-bar {
    max-width: 100%;
  }
  
  .digit {
    font-size: 1.25rem;
    min-width: 1.2rem;
  }
  
  .operator-input {
    width: 1.35rem;
    height: 1.7rem;
    font-size: 1.15rem;
    border-bottom-width: 2px;
  }

  :root {
    --digit-size: 1.25rem;
    --operator-size: 1.15rem;
  }

  .operator-btn {
    height: 70px;
    font-size: 2rem;
  }

  .footer {
    display: none;
  }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Print */
@media print {
  .operator-buttons,
  .modal,
  .controls {
    display: none !important;
  }
}
