/*
  The stylesheet of the game page (index.html) and the Help page
  (help.html). It follows dancroak.com/web/css: six cascade layers in
  one order, the shared token names, class rules only in components and
  utilities, and width breakpoints written as ranges. A page carries no
  style element.

  The two pages share no rule from the body down, so each body wears a
  page class: .game-page or .help-page. The Help page has a light
  theme: .help-page sets its own values of the color tokens.

  The sheet styles a class, not an ID. An element keeps its ID only
  when main.js reads it.

  main.js writes two custom properties inline and no other style:
  --zone--color on each board cell and habitat square, and --hunger--pct
  on each hunger bar.
*/

@layer reset, tokens, base, components, utilities, print;

@layer reset {
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }
}

@layer tokens {
  :root {
    /* The family chrome: Duck Duck Bay and Marshfall share these.
       Each game's identity lives inside its play area. */
    --color--bg: #050d22;
    --color--bg-secondary: #101a33;
    --color--bg-secondary-hover: #16223f;
    --color--accent: #2f70dd;
    --color--accent-hover: #275fc0;
    --color--gold: #c4a35a;
    --color--text: #f4f7ff;
    --color--text-light: #8b98b8;
    --color--border-base: #333;
    --font-family--base:
      -apple-system,
      BlinkMacSystemFont,
      "Segoe UI",
      Roboto,
      Helvetica,
      Arial,
      sans-serif;
    --spacing--medium: 1rem;
  }
}

@layer components {
  /* Game page */
  .game-page {
    font-family: var(--font-family--base);
    background: var(--color--bg);
    color: var(--color--text);
    min-height: 100vh;
    display: flex;
    justify-content: center;
  }

  .game-page header {
    text-align: center;
    margin-bottom: 1rem;
  }

  .game-page header h1 {
    font-size: 0.85rem;
    font-weight: 600;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--color--text);
    margin-bottom: 0.5rem;
  }

  .game-page header h1::after {
    content: "";
    display: block;
    width: 24px;
    height: 2px;
    margin: 6px auto 0;
    background: var(--color--gold);
  }

  .game-page footer {
    text-align: center;
    margin-top: 0.25rem;
  }

  .game-page footer p {
    font-size: 0.7rem;
    font-weight: 300;
    color: var(--color--gold);
  }

  .game-page footer a {
    color: var(--color--gold);
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-color: var(--color--gold);
    text-underline-offset: 2px;
    transition: color 0.2s, text-decoration-color 0.2s;
  }

  .game-page footer a:hover {
    color: var(--color--gold);
    text-decoration-style: solid;
    text-decoration-color: var(--color--gold);
  }

  @media (width >= 600px) {
    .game-page header h1 {
      font-size: 2rem;
    }
  }

  /* Help page */
  .help-page {
    --color--text: #2c3e50;
    --color--text-light: #6c757d;
    --color--bg: #f8f9fa;
    --color--link: #1a4a6e;
    --font-family--base:
      -apple-system,
      BlinkMacSystemFont,
      "Segoe UI",
      Roboto,
      sans-serif;
    font-family: var(--font-family--base);
    line-height: 1.6;
    color: var(--color--text);
    background: var(--color--bg);
    padding: 20px;
    max-width: 800px;
    margin: 0 auto;
  }

  .help-page header {
    text-align: center;
    margin: 2rem 0 3rem;
  }

  .help-page h1 {
    font-size: 2rem;
    margin-bottom: 0.5rem;
  }

  .help-page h1 a {
    color: var(--color--text);
    text-decoration: none;
  }

  .help-page h1 a:hover {
    text-decoration: underline;
  }

  .help-page h2 {
    font-size: 1.5rem;
    margin: 2rem 0 1rem 0;
    padding-bottom: 0.5rem;
  }

  .help-page p,
  .help-page ul,
  .help-page ol {
    margin-bottom: 1rem;
  }

  .help-page ul,
  .help-page ol {
    margin-left: 2rem;
  }

  .help-page li {
    margin-bottom: 0.5rem;
  }

  .help-page a {
    color: var(--color--link);
    text-decoration: none;
  }

  .help-page a:hover {
    text-decoration: underline;
  }

  .help-page footer {
    margin: 3rem 0 2rem;
    text-align: center;
    color: var(--color--text-light);
    font-size: 0.9rem;
  }

  .help-page footer a {
    color: var(--color--link);
    text-decoration: none;
  }

  .help-page footer a:hover {
    text-decoration: underline;
  }

  @media (width <= 600px) {
    .help-page {
      padding: 15px;
    }

    .help-page h1 {
      font-size: 1.5rem;
    }

    .help-page h2 {
      font-size: 1.3rem;
    }
  }

  /* Game page parts */
  .app {
    width: 100%;
    max-width: 500px;
    padding: 1rem;
  }

  @media (width >= 600px) {
    .app {
      padding: 2rem;
    }
  }

  .victory-badge {
    color: #ffd700;
    font-weight: 600;
    font-size: 1.2rem;
    margin-top: 0.5rem;
    animation: victoryPulse 2s ease-in-out infinite;
  }

  @keyframes victoryPulse {
    0%,
    100% {
      opacity: 1;
    }
    50% {
      opacity: 0.7;
    }
  }

  /* Help drawer */
  .drawer-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 90;
  }

  .drawer-overlay.open {
    opacity: 1;
    pointer-events: auto;
  }

  /* Desktop: allow playing while the help drawer is open */
  @media (width >= 900px) {
    .drawer-overlay.open {
      opacity: 0;
      pointer-events: none;
    }
  }

  .drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 90%;
    max-width: 400px;
    background: var(--color--bg-secondary);
    box-shadow: -2px 0 20px rgba(0, 0, 0, 0.3);
    transform: translateX(100%);
    transition: transform 0.2s ease-out;
    z-index: 100;
    overflow-y: auto;
    padding: 1.5rem;
  }

  .drawer.open {
    transform: translateX(0);
  }

  .drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--color--border-base);
    position: sticky;
    top: -1.5rem;
    background: var(--color--bg-secondary);
    padding-top: 1.5rem;
    margin-top: -1.5rem;
    z-index: 1;
  }

  .drawer-header h2 {
    font-size: 1.25rem;
    color: #fff;
  }

  .drawer-close {
    background: none;
    border: none;
    color: #999;
    font-size: 2rem;
    cursor: pointer;
    padding: 0;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s;
    line-height: 1;
  }

  .drawer-close:hover {
    color: #fff;
  }

  .chain-section {
    font-size: 0.6rem;
    color: #555;
    text-align: center;
    margin-top: 0.5rem;
    margin-bottom: 0.15rem;
  }

  .chain-section:first-of-type {
    margin-top: 1.5rem;
  }

  .chain-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.1rem;
    margin: 0.2rem 0;
    font-size: 1.4rem;
  }

  .chain-arrow {
    color: #555;
    font-size: 0.8rem;
    margin: 0 0.15rem;
  }

  .chain-item {
    opacity: 0.3;
    transition: opacity 0.3s;
  }

  .chain-item.achieved {
    opacity: 1;
  }

  .help-section {
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--color--border-base);
  }

  .help-section h3 {
    color: #fff;
    font-size: 1rem;
    margin-bottom: 0.75rem;
  }

  .help-section h4 {
    color: var(--color--text-light);
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
    font-weight: 500;
  }

  /* Ecosystem Guide spacing */
  .help-section.help-section--guide {
    margin-top: 2rem;
  }

  .help-section p {
    color: #aaa;
    font-size: 0.85rem;
    line-height: 1.5;
    margin-bottom: 0.5rem;
  }

  .help-section p strong {
    color: var(--color--text-light);
  }

  .help-section a {
    color: #777;
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-color: #555;
    text-underline-offset: 2px;
    transition: color 0.2s, text-decoration-color 0.2s;
  }

  .help-section a:hover {
    color: #aaa;
    text-decoration-style: solid;
    text-decoration-color: #888;
  }

  .guide-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
    color: var(--color--text-light);
    font-size: 0.9rem;
  }

  .guide-row select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background: var(--color--bg-secondary);
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns=\"http://www.w3.org/2000/svg\" width=\"12\" height=\"12\" viewBox=\"0 0 12 12\"%3e%3cpath fill=\"%23999\" d=\"M6 9L1 4h10z\"/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 0.5rem center;
    background-size: 12px;
    color: #fff;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 0.4rem 2rem 0.4rem 0.6rem;
    font-size: 0.9rem;
    cursor: pointer;
    transition: border-color 0.2s;
  }

  .guide-row select:hover {
    border-color: #666;
  }

  .guide-row select:focus {
    outline: none;
    border-color: #888;
  }

  .guide-expl {
    color: #999;
    font-size: 0.85rem;
    font-style: italic;
    line-height: 1.4;
  }

  /* Board */
  .board-container {
    width: 100%;
    aspect-ratio: 1;
    margin-bottom: 1rem;
  }

  .board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    gap: 2px;
    width: 100%;
    height: 100%;
    background: #333;
    border-radius: 8px;
    overflow: hidden;
  }

  .cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.1s;
    position: relative;
    background-color: var(--zone--color);
  }

  .cell:active {
    transform: scale(0.95);
  }

  .cell.selected {
    box-shadow: inset 0 0 0 3px #fff;
  }

  .emoji {
    font-size: clamp(1.2rem, 5vw, 2rem);
    line-height: 1;
  }

  .hunger-bar {
    position: absolute;
    bottom: 2px;
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    height: 4px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 2px;
    overflow: hidden;
  }

  .hunger-fill {
    width: var(--hunger--pct);
    height: 100%;
    background: linear-gradient(90deg, #ffeb3b 0%, #ff9800 50%, #ff6b6b 100%);
    transition: width 0.3s ease;
    border-radius: 2px;
  }

  /* Info panel */
  .info-panel {
    background: var(--color--bg-secondary);
    border-radius: 8px;
    padding: 0.75rem;
    margin-bottom: 0.75rem;
    min-height: 60px;
  }

  .info-content {
    font-size: 0.9rem;
    line-height: 1.5;
    color: var(--color--text-light);
  }

  .info-content strong {
    color: #fff;
    font-size: 1rem;
  }

  .info-title {
    margin-bottom: 0.5rem;
  }

  .info-title.info-title--move {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .info-line {
    font-size: 0.9rem;
    color: var(--color--text-light);
  }

  .zone-square {
    display: inline-block;
    width: 0.75rem;
    height: 0.75rem;
    border-radius: 2px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    vertical-align: middle;
    background-color: var(--zone--color);
  }

  .learn-more-link {
    color: #777;
    text-decoration: none;
    font-size: 0.85rem;
    transition: color 0.2s;
    display: inline-block;
    margin-top: 0.25rem;
  }

  .learn-more-link:hover {
    color: #aaa;
    text-decoration: underline;
  }

  .move-indicator {
    font-size: 0.7rem;
    color: #666;
    font-weight: 400;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  /* Controls */
  .controls {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
  }

  .controls button {
    flex: 1;
    padding: 0.75rem;
    font-size: 1rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
    font-weight: 500;
    background: var(--color--bg-secondary);
    color: var(--color--text);
  }

  .controls button:hover:not(:disabled) {
    background: var(--color--bg-secondary-hover);
  }

  .controls button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  /* Event log */
  .event-log {
    position: relative;
    background: var(--color--bg-secondary);
    border-radius: 8px;
    padding: 0.75rem;
    max-height: 20vh;
    overflow-y: auto;
  }

  /* Log header (score + goal meter) */
  .log-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--color--border-base);
    font-size: 0.9rem;
  }

  .copy-log-btn {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    background: none;
    border: none;
    color: #666;
    cursor: pointer;
    padding: 0.25rem;
    width: 1.5rem;
    height: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: color 0.2s, background 0.2s;
    opacity: 0;
    z-index: 10;
  }

  .event-log:hover .copy-log-btn {
    opacity: 1;
  }

  .copy-log-btn:hover {
    color: #aaa;
    background: #333;
  }

  /* main.js adds copied for 200ms after a copy. */
  .copy-log-btn.copied {
    color: #8f8;
  }

  .copy-log-btn:active {
    transform: scale(0.95);
  }

  .copy-log-btn svg {
    width: 100%;
    height: 100%;
  }

  .points {
    color: var(--color--text);
    font-weight: 500;
  }

  .deadline {
    color: #888;
    font-size: 0.8rem;
    white-space: nowrap;
  }

  .deadline.urgent {
    color: #e8955a;
    font-weight: 600;
  }

  .goal-meter {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 1.1rem;
    margin-left: auto;
  }

  .goal-item {
    opacity: 0.3;
    transition: opacity 0.3s;
  }

  .goal-item.achieved {
    opacity: 1;
  }

  .goal-item.dragon {
    font-size: 1.3rem;
  }

  /* Dragon progress states */
  /* 50-75% progress - warming up */
  .goal-item.dragon-heating {
    opacity: 0.6;
    filter: brightness(1.1);
  }

  /* 75%+ progress - getting hot */
  .goal-item.dragon-hot {
    opacity: 0.85;
    filter: brightness(1.3);
    animation: dragonPulse 2s ease-in-out infinite;
  }

  /* 100% progress - ready to win! */
  .goal-item.dragon-ready {
    opacity: 1;
    filter: brightness(1.5);
    animation: dragonGlow 1.5s ease-in-out infinite;
  }

  @keyframes dragonPulse {
    0%,
    100% {
      transform: scale(1);
      opacity: 0.85;
    }
    50% {
      transform: scale(1.05);
      opacity: 1;
    }
  }

  @keyframes dragonGlow {
    0%,
    100% {
      transform: scale(1);
      filter: brightness(1.5) drop-shadow(0 0 2px #ffd700);
    }
    50% {
      transform: scale(1.1);
      filter: brightness(1.7) drop-shadow(0 0 4px #ffd700);
    }
  }

  .goal-arrow {
    color: #555;
    font-size: 0.8rem;
    margin: 0 0.1rem;
  }

  .events {
    position: relative;
    font-size: 0.85rem;
    line-height: 1.4;
  }

  .turn-group {
    margin-bottom: 0.75rem;
    animation: fadeIn 0.3s;
  }

  .turn-header {
    font-weight: 600;
    color: #888;
    font-size: 0.75rem;
    margin-bottom: 0.25rem;
    text-transform: uppercase;
  }

  .event {
    padding: 0.15rem 0;
    padding-left: 0.5rem;
    border-left: 2px solid #444;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(-5px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  /* Game over overlay */
  .overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
  }

  .overlay-content {
    background: var(--color--bg-secondary);
    padding: 2rem;
    border-radius: 16px;
    text-align: center;
    max-width: 300px;
  }

  .overlay-title {
    font-size: 1.5rem;
    margin-bottom: 1rem;
  }

  .overlay-message {
    color: #aaa;
    margin-bottom: 1.5rem;
  }

  .overlay-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  .overlay-buttons button {
    border: none;
    padding: 0.75rem 1.5rem;
    font-size: 1rem;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.2s;
  }

  .btn-primary {
    background: #2d5a27;
    color: #fff;
  }

  .btn-primary:hover {
    background: #3d7a37;
  }

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

  .btn-secondary:hover {
    background: var(--color--bg-secondary-hover);
  }

  /* Loading */
  .loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--color--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
  }

  .loading p {
    font-size: 1.2rem;
    color: #aaa;
  }
}

@layer utilities {
  .mt-1 {
    margin-top: var(--spacing--medium);
  }

  .hidden {
    display: none !important;
  }
}
