:root {
  /* Default Light Theme */
  --bg-page: #f8fafc; /* slate-50 */
  --bg-panel: #ffffff;
  --ink-main: #0f172a; /* slate-900 */
  --ink-muted: #64748b; /* slate-500 */
  --accent-primary: #2563eb; /* blue-600 */
  --accent-hover: #1d4ed8;
  
  --cell-bg: #ffffff;
  --cell-border: rgba(100, 116, 139, 0.2);
  --block-bg: #1e293b; /* slate-800 */
  --block-text: #f8fafc;
  --run-highlight: #dbeafe; /* blue-100 */
  --active-highlight: #bfdbfe; /* blue-200 */
  
  --val-success: #16a34a;
  --val-error: #dc2626;
  
  --shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.05);
  
  --font-serif: 'Merriweather', Georgia, serif;
  --font-sans: 'DM Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  
  --scaler: 1; /* Used for Accessibility Mode */
}

/* Dark Theme */
[data-theme="dark"] {
  --bg-page: #020617; /* slate-950 */
  --bg-panel: #0f172a;
  --ink-main: #f8fafc;
  --ink-muted: #94a3b8;
  --accent-primary: #60a5fa; /* blue-400 */
  --accent-hover: #93c5fd;
  
  --cell-bg: #0f172a;
  --cell-border: rgba(148, 163, 184, 0.2);
  --block-bg: #1e293b;
  --block-text: #f8fafc;
  --run-highlight: rgba(30, 58, 138, 0.4);
  --active-highlight: rgba(30, 58, 138, 0.8);
}

/* Sepia Theme */
[data-theme="sepia"] {
  --bg-page: #f4ecd8;
  --bg-panel: #ffffff;
  --ink-main: #5b4636;
  --ink-muted: #8b7355;
  --accent-primary: #c96a4e;
  --accent-hover: #a8543b;
  
  --cell-bg: #fffcf8;
  --cell-border: rgba(139, 115, 85, 0.2);
  --block-bg: #5b4636;
  --block-text: #f4ecd8;
  --run-highlight: #f0e4c8;
  --active-highlight: #e6d8b3;
}

[data-size="large"] {
  --scaler: 1.25;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-sans);
  color: var(--ink-main);
  background-color: var(--bg-page);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.4s ease, color 0.4s ease;
}

button, a, input {
  font: inherit;
  color: inherit;
}

.page-shell {
  max-width: 1200px;
  margin: 0 auto;
}

.text-muted { color: var(--ink-muted); }

/* Typography */
h1, h2, .brand-title {
  font-family: var(--font-sans);
  margin: 0;
  color: var(--accent-primary);
  font-weight: 900;
  letter-spacing: -0.05em;
}

.brand-title {
  font-size: 1.25rem;
}

h2 {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  color: var(--ink-main);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--ink-muted);
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-size: 0.75rem;
}

/* Masthead (Open Layout) */
.masthead {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--cell-border);
  margin-bottom: 32px;
}

.masthead-left, .masthead-right {
  display: flex;
  align-items: center;
  gap: 24px;
}

/* Theme Switcher */
.theme-switcher {
  display: flex;
  background: rgba(0,0,0,0.05);
  border-radius: 8px;
  padding: 4px;
}
[data-theme="dark"] .theme-switcher { background: rgba(255,255,255,0.1); }

.theme-btn {
  padding: 6px;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  border: none;
  background: transparent;
  opacity: 0.6;
}
.theme-btn:hover { opacity: 1; }
.theme-btn.active {
  background: var(--bg-panel);
  opacity: 1;
  box-shadow: var(--shadow-sm);
}

/* App Grid Structure */
.app-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.8fr) minmax(280px, 1fr);
  gap: 60px;
  align-items: center;
  padding: 0 24px 60px;
}

.game-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.message-banner {
  font-size: 0.9rem;
  color: var(--ink-muted);
  min-height: 24px;
  margin-bottom: 16px;
  font-weight: 500;
}

/* The Board */
.board-container {
  width: 100%;
  max-width: calc(500px * var(--scaler));
  margin-bottom: 32px;
  transition: max-width 0.3s ease;
}

.board {
  display: grid;
  gap: 1px;
  background: var(--ink-main);
  border: 4px solid var(--ink-main);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}
[data-theme="dark"] .board { box-shadow: 0 20px 40px rgba(0,0,0,0.5); }

.board:focus { outline: none; }

.cell {
  aspect-ratio: 1;
  min-width: 32px;
  position: relative;
  background: var(--cell-bg);
  transition: background 150ms ease;
}

.cell.play {
  display: grid;
  place-items: center;
  cursor: pointer;
}

/* Interactions */
.cell.play:hover {
  background: var(--run-highlight);
}

.cell.play.related {
  background: var(--run-highlight);
}

.cell.play.active {
  background: var(--active-highlight);
  box-shadow: inset 0 0 0 2px var(--accent-primary);
  z-index: 2;
}

.cell.play.correct .value { color: var(--val-success); }
.cell.play.invalid {
  background: rgba(220, 38, 38, 0.1);
  box-shadow: inset 0 0 0 2px var(--val-error);
}
.cell.play.invalid .value { color: var(--val-error); }

/* Block & Clues */
.cell.block {
  background: var(--block-bg);
  color: var(--block-text);
}

.cell.block.split {
  background: 
    linear-gradient(to bottom right, 
      transparent calc(50% - 1px), 
      rgba(255,255,255,0.2) 50%, 
      transparent calc(50% + 1px)
    ),
    var(--block-bg);
}

.value {
  font-size: calc(1.8rem * var(--scaler));
  font-weight: 500;
  color: var(--ink-main);
}

/* Notes rendering */
.notes-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 4px;
}

.note-digit {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(0.6rem * var(--scaler));
  color: var(--ink-muted);
  line-height: 1;
}

/* Clue positioning */
.clue {
  position: absolute;
  font-size: calc(1.1rem * var(--scaler));
  line-height: 1;
  font-weight: 700;
}
.clue.down { top: 6px; right: 8px; }
.clue.across { bottom: 6px; left: 8px; }

/* Controls & Keypad */
.sticky-footer {
  display: flex;
  flex-direction: column;
  gap: 24px;
  width: 100%;
  max-width: calc(600px * var(--scaler));
}

.control-strip {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
}

.divider {
  width: 1px;
  height: 24px;
  background: var(--cell-border);
  margin: 0 4px;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  width: 100%;
}

@media (min-width: 500px) {
  .keypad { grid-template-columns: repeat(10, 1fr); }
}

/* Buttons */
button {
  cursor: pointer;
  border: none;
  background: transparent;
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 700;
  transition: all 150ms ease;
  user-select: none;
  touch-action: manipulation;
}

.chip-row {
  display: flex;
  gap: 8px;
}

.chip {
  padding: 6px 14px;
  background: var(--bg-panel);
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--ink-muted);
  font-weight: 700;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
.chip:hover { background: rgba(0,0,0,0.03); border-color: var(--accent-primary); color: var(--accent-primary); }
[data-theme="dark"] .chip:hover { background: rgba(255,255,255,0.05); }

.chip.active {
  background: var(--accent-primary);
  color: white;
  border-color: var(--accent-primary);
}

.primary-button {
  background: var(--accent-primary);
  color: white;
}
.primary-button:hover { background: var(--accent-hover); }

.primary-button.outline {
  background: transparent;
  color: var(--accent-primary);
  border: 2px solid var(--accent-primary);
}
.primary-button.outline:hover {
  background: var(--accent-primary);
  color: white;
}

.outline-button {
  background: transparent;
  border: 1px solid var(--cell-border);
}
.outline-button:hover {
  border-color: var(--ink-main);
  color: var(--ink-main);
}

.ghost-button, .ghost-link {
  color: var(--ink-muted);
  text-decoration: none;
  font-weight: 500;
}
.ghost-button:hover, .ghost-link:hover {
  color: var(--ink-main);
  background: rgba(0,0,0,0.04);
}
[data-theme="dark"] .ghost-button:hover {
  background: rgba(255,255,255,0.1);
}

.icon-button {
  background: var(--bg-panel);
  border: 1px solid var(--cell-border);
  font-size: 0.9rem;
  color: var(--ink-main);
}
.icon-button:hover {
  border-color: var(--accent-primary);
}
.icon-button.active {
  background: var(--accent-primary);
  color: white;
  border-color: var(--accent-primary);
}

.primary-link {
  display: inline-block;
  background: var(--accent-primary);
  color: white;
  text-decoration: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 700;
  margin-top: 16px;
}
.primary-link:hover { background: var(--accent-hover); }

/* Keypad Keys */
.key {
  aspect-ratio: 1;
  background: var(--bg-panel);
  border: 2px solid var(--cell-border);
  border-radius: 12px;
  font-size: calc(1.5rem * var(--scaler));
  font-weight: 400;
  color: var(--ink-main);
  box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}
[data-theme="dark"] .key { box-shadow: 0 4px 6px rgba(0,0,0,0.3); }

.key:hover {
  border-color: var(--accent-primary);
  color: var(--accent-primary);
  transform: translateY(-2px);
}
.key:active {
  transform: translateY(2px);
  box-shadow: none;
}

.key.action-key {
  font-size: calc(0.9rem * var(--scaler));
  font-weight: 700;
  background: rgba(0,0,0,0.02);
}
[data-theme="dark"] .key.action-key { background: rgba(255,255,255,0.05); }

/* Sidebar (Open Layout) */
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 48px;
}

.info-block {
  padding-bottom: 24px;
}

.info-block p {
  color: var(--ink-muted);
  font-size: 1.1rem;
}

.meta-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin: 16px 0;
  font-size: 1rem;
}
.meta-list dt { color: var(--ink-muted); }
.meta-list dd { margin: 0; font-weight: 700; text-align: right; }

.mode-text-container {
  padding-top: 16px;
  border-top: 1px solid var(--cell-border);
  display: flex;
  justify-content: space-between;
  font-size: 1rem;
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 1;
  transition: opacity 200ms ease;
}
.modal-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: var(--bg-panel);
  padding: 48px;
  border-radius: 24px;
  text-align: center;
  max-width: 400px;
  box-shadow: 0 25px 50px rgba(0,0,0,0.25);
  border: 4px solid var(--accent-primary);
}

/* Mobile Adjustments */
.show-mobile { display: none; }
.top-help-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

@media (max-width: 800px) {
  .app-grid {
    grid-template-columns: 1fr;
    gap: 32px;
    padding: 0 16px 80px;
  }
  
  .hidden-mobile { display: none !important; }
  .show-mobile { 
    display: flex; 
    justify-content: center;
    margin-bottom: 16px;
  }
  .side-panel #howToBtn {
    display: none;
  }
  
  .sticky-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: 100%;
    background: var(--bg-panel);
    padding: 16px;
    border-top: 1px solid var(--cell-border);
    z-index: 50;
    box-shadow: 0 -4px 12px rgba(0,0,0,0.05);
  }

  .control-strip {
    overflow-x: auto;
    padding-bottom: 4px;
    justify-content: flex-start;
    -webkit-overflow-scrolling: touch;
  }
}

/* Pixel Wizard Press top brand bar */
.pw-topbar { background: #0f172a; color: #e2e8f0; font-size: 13px; }
.pw-topbar-inner { max-width: 1200px; margin: 0 auto; padding: 8px 16px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.pw-topbar-brand { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; font-weight: 600; }
.pw-topbar-brand:hover { color: #fff; }
.pw-topbar-brand img { display: block; }
.pw-topbar-nav { display: flex; align-items: center; gap: 18px; }
.pw-topbar-nav a { color: inherit; text-decoration: none; }
.pw-topbar-nav a:hover { color: #fff; }
@media (max-width: 560px) { .pw-hide-sm { display: none; } .pw-topbar { font-size: 12px; } }

/* Pixel brand title override */
.brand-link { display: inline-flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; }
.brand-link .brand-icon { height: 36px; width: 36px; object-fit: contain; flex-shrink: 0; }
.brand-title { font-family: "Press Start 2P", monospace !important; font-size: 14px !important; line-height: 1 !important; letter-spacing: 0 !important; color: var(--accent-primary, #2563eb) !important; }
@media (max-width: 560px) { .brand-title { font-size: 10px !important; } .brand-link .brand-icon { height: 28px; width: 28px; } }

/* Topbar theme-aware overrides (match kakuro's own theme) */
html[data-theme="light"] .pw-topbar { background: #f1f5f9; color: #334155; border-bottom: 1px solid #e2e8f0; }
html[data-theme="sepia"] .pw-topbar { background: #e6d8b3; color: #5b4636; border-bottom: 1px solid #d4c5a0; }
/* dark is the default styled above */
