/**
 * main.css — Base layout, typography, navigation, accessibility, and utility styles.
 */

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 100%;
  -webkit-text-size-adjust: 100%;
  tab-size: 4;
  width: 100%;
}

body {
  background-color: var(--slate-mist);
  background-image: var(--pattern-loom-grid);
  background-size: var(--pattern-grid-size);
  color: var(--iron-gall);
  font-family: var(--font-ui);
  font-size: var(--step-0);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  width: 100%;
}

/* --- Keyboard Accessibility: Skip to Content --- */
.skip-link {
  position: absolute;
  top: -100px;
  left: var(--space-m);
  background: var(--iron-gall);
  color: var(--paper-light);
  padding: var(--space-xs) var(--space-m);
  font-weight: 600;
  text-decoration: none;
  border-radius: var(--radius-subtle);
  z-index: 9999;
  transition: top var(--trans-fast);
}

.skip-link:focus { top: var(--space-m); outline: 3px solid var(--cobalt-yarn); outline-offset: 2px; }

/* --- Global Focus Indicators --- */
:focus-visible { outline: 3px solid var(--focus-ring); outline-offset: var(--focus-offset); }

/* --- Screen Reader Only Utility --- */
.visually-hidden {
  position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important;
  margin: -1px !important; overflow: hidden !important; clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important; border: 0 !important;
}

/* --- Typography Hierarchy --- */
h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--iron-gall);
  line-height: 1.15;
  letter-spacing: -0.02em;
}

h1 { font-size: var(--step-3); } h2 { font-size: var(--step-2); }
h3 { font-size: var(--step-1); } h4 { font-size: var(--step-0); }

p {
  max-width: 65ch;
}

/* --- Buttons & Interactive Elements --- */
button {
  font-family: inherit;
  font-size: var(--step-0);
  border: none;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.btn-primary,
.btn-danger {
  padding: var(--space-xs) var(--space-m);
  font-weight: 700;
  font-size: var(--step-0);
  border-radius: var(--radius-chip);
  border: 1px solid var(--iron-gall);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #FFFFFF;
  transition: background var(--trans-fast);
}

.btn-primary { background: var(--cobalt-yarn); }
.btn-primary:hover:not(:disabled) { background: #15397A; }
.btn-danger { background: var(--cinnabar); }
.btn-danger:hover:not(:disabled) { background: #871D1B; }

/* --- Noscript Fallback Warning --- */
.noscript-warning {
  padding: var(--space-xl);
  background: var(--slate-mist);
  color: var(--iron-gall);
  border-bottom: 2px solid var(--iron-gall);
}

/* --- App Shell Container --- */
.app-shell {
  width: 100%;
  max-width: 82rem;
  margin: 0 auto;
  padding: var(--space-s) var(--space-m) var(--space-xl);
  display: flex;
  flex-direction: column;
  gap: var(--space-m);
  flex: 1;
}

/* --- App Header & Navigation --- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 2px solid var(--iron-gall);
  padding-bottom: var(--space-xs);
  flex-wrap: wrap;
  gap: var(--space-s);
}

.brand-badge {
  display: flex;
  align-items: baseline;
  gap: var(--space-xs);
}

.brand-title {
  font-family: var(--font-display);
  font-size: var(--step-2);
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--iron-gall);
  text-transform: uppercase;
}

.brand-game {
  font-family: var(--font-ui);
  font-size: var(--step--1);
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--slate-neutral);
  text-transform: uppercase;
}

/* Primary Top Navigation Tabs */
.app-nav {
  display: flex;
  align-items: center;
}

.nav-tab-list {
  display: flex;
  list-style: none;
  gap: 4px;
  background: rgba(24, 29, 32, 0.06);
  padding: 3px;
  border-radius: var(--radius-chip);
  border: 1px solid var(--draft-grid);
}

.nav-tab-btn {
  padding: 6px 14px;
  font-size: var(--step--1);
  font-weight: 600;
  color: var(--iron-gall);
  border-radius: calc(var(--radius-chip) - 2px);
  min-height: 36px;
  transition: all var(--trans-fast);
}

.nav-tab-btn:hover {
  background: rgba(255, 255, 255, 0.6);
}

.nav-tab-btn.is-active,
.nav-tab-btn[aria-selected="true"] {
  background: var(--paper-light);
  color: var(--cobalt-yarn);
  box-shadow: 0 1px 3px rgba(24, 29, 32, 0.12);
  font-weight: 700;
}

/* Header Tools & Sound Toggle */
.header-tools {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  flex-wrap: wrap;
}

.sound-toggle-btn,
.tutorial-toggle-btn,
.preferences-toggle-btn,
.nav-btn {
  font-size: var(--step--1);
  font-weight: 600;
  padding: 4px 10px;
  border-radius: var(--radius-chip);
  border: 1px solid var(--draft-grid);
  color: var(--iron-gall);
  background: rgba(24, 29, 32, 0.04);
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  transition: all var(--trans-fast);
}

.sound-toggle-btn:hover,
.tutorial-toggle-btn:hover,
.preferences-toggle-btn:hover,
.nav-btn:hover:not(:disabled) {
  background: var(--paper-light);
  border-color: var(--iron-gall);
}

.nav-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.puzzle-nav {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.nav-label {
  font-size: var(--step--1);
  font-weight: 600;
  color: var(--iron-gall);
  padding: 0 4px;
  white-space: nowrap;
}

.tutorial-toggle-btn {
  background: var(--grad-surface);
  border-color: var(--cobalt-yarn);
  color: var(--cobalt-yarn);
  font-weight: 700;
}

.sound-toggle-btn[aria-pressed="true"] {
  background: rgba(30, 76, 161, 0.08);
  border-color: var(--cobalt-yarn);
  color: var(--cobalt-yarn);
  font-weight: 700;
}

/* --- View Toggle Invariants --- */
.app-view {
  display: none;
  width: 100%;
}

.app-view.is-active {
  display: block;
}

/* --- Shared View Headers --- */
.view-header-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-m);
  padding-bottom: var(--space-s);
  border-bottom: 1px solid var(--draft-grid);
  flex-wrap: wrap;
  gap: var(--space-xs);
}

.view-heading { font-size: var(--step-2); margin-bottom: 2px; }
.view-subtext { font-size: var(--step-0); color: var(--slate-neutral); }

/* --- Global Hidden Attribute Invariant --- */
[hidden] { display: none !important; }

/* --- Modal Overlay & Dialog --- */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(24, 29, 32, 0.6);
  backdrop-filter: blur(2px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: var(--space-m);
}
.modal-overlay:not([hidden]) { display: flex; }

.modal-box {
  background: var(--paper-light);
  border: 2px solid var(--iron-gall);
  border-radius: var(--radius-card);
  max-width: 480px;
  width: 100%;
  padding: var(--space-l);
  display: flex;
  flex-direction: column;
  gap: var(--space-s);
  box-shadow: 0 8px 32px rgba(24, 29, 32, 0.2);
}

.modal-head-row { display: flex; align-items: center; justify-content: space-between; gap: var(--space-s); }
.modal-close-icon { font-size: 24px; line-height: 1; color: var(--slate-neutral); padding: 4px 8px; border-radius: var(--radius-chip); }
.modal-close-icon:hover { color: var(--iron-gall); background: rgba(24, 29, 32, 0.08); }
.modal-title { font-size: var(--step-1); color: var(--iron-gall); }
.modal-body { font-size: var(--step-0); line-height: 1.45; color: var(--iron-gall); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-xs);
  margin-top: var(--space-xs);
}

/* --- App Footer --- */
.app-footer {
  margin-top: auto;
  padding-top: var(--space-m);
  border-top: 1px solid var(--draft-grid);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-s);
}
.footer-meta { display: flex; flex-direction: column; gap: 2px; }
.footer-brand { font-family: var(--font-display); font-size: var(--step--1); font-weight: 700; color: var(--iron-gall); }
.footer-subtext { font-size: var(--step--2); color: var(--slate-neutral); }
.footer-links { display: flex; align-items: center; gap: var(--space-xs); flex-wrap: wrap; }
.footer-link-btn { font-size: var(--step--1); font-weight: 600; color: var(--iron-gall); padding: 4px 8px; border-radius: var(--radius-chip); }
.footer-link-btn:hover { background: var(--paper-light); color: var(--cobalt-yarn); }

/* --- Live Region for Screen Readers --- */
#sr-announcer {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* --- Reduced Motion & High Contrast Ergonomics --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

html[data-reduced-motion="true"] *,
html[data-reduced-motion="true"] *::before,
html[data-reduced-motion="true"] *::after {
  animation-duration: 0.001ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.001ms !important;
  scroll-behavior: auto !important;
}

html[data-high-contrast="true"] .game-object-svg circle {
  stroke-width: 2.5px !important;
}
