:root {
    --bg: #161514;
    --paper: #faf6ee;
    --ink: #1e1b18;
    --ui-bg: #22201e;
    --ui-border: #383532;
    --ui-text: #d8d2c7;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body {
    background: var(--bg);
    color: var(--ui-text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    max-height: 100vh;
    overflow: hidden;
    padding: 10px 16px;
    gap: 10px;
  }
  .toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 760px;
    flex: 0 0 auto;
  }
  input[type="text"] {
    background: var(--ui-bg);
    color: var(--ui-text);
    border: 1px solid var(--ui-border);
    padding: 7px 11px;
    font-family: inherit;
    font-size: 13px;
    width: 240px;
    max-width: 100%;
    outline: none;
  }
  input[type="text"]:focus { border-color: #7c756c; }
  button {
    background: var(--ui-bg);
    color: var(--ui-text);
    border: 1px solid var(--ui-border);
    padding: 7px 12px;
    font-family: inherit;
    font-size: 12px;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s;
  }
  button:hover { background: #2e2b28; border-color: #635e57; }
  button:active { transform: translateY(1px); }
  button:disabled { opacity: 0.35; cursor: default; }
  button:disabled:hover { background: var(--ui-bg); border-color: var(--ui-border); }

  /* Toolbar order: single mode = seed, Generate, Random, Redraw.
     grid mode = size, Random, Redraw (seed/Generate are hidden). */
  #seedInput { order: 1; }
  #btnGenerate { order: 2; }
  #btnRandom { order: 3; }
  #btnAnimate { order: 4; }
  #gridSizeWrap { order: 0; }

  .content {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    column-gap: 14px;
    width: 100%;
    min-height: 0;
  }
  .modetabs {
    justify-self: end;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
  }
  .movenavs {
    justify-self: start;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
  }
  .mode-btn.active { background: #3a3632; border-color: #8a8178; color: #fff; }
  body.mode-grid #seedInput,
  body.mode-grid #btnGenerate { display: none; }
  body:not(.mode-grid) #gridSizeWrap { display: none; }

  select {
    background: var(--ui-bg);
    color: var(--ui-text);
    border: 1px solid var(--ui-border);
    padding: 7px 8px;
    font-family: inherit;
    font-size: 12px;
    cursor: pointer;
    outline: none;
  }
  g[data-seed] { cursor: pointer; }
  g[data-seed]:hover { opacity: 0.9; }
  .stage {
    --stage-ratio: 0.863636;
    width: min(760px, calc(100vw - 170px), calc((100vh - 165px) * (760 / 880)));
    aspect-ratio: 760 / 880;
    background: var(--paper);
    box-shadow: 0 18px 50px rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 1 auto;
    min-height: 0;
    justify-self: center;
  }
  svg { width: 100%; height: 100%; display: block; }

  .footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 100%;
    flex: 0 0 auto;
    min-width: 0;
  }
  .meta {
    margin-top: 0;
    font-size: 11px;
    color: #7d776f;
    text-align: center;
    letter-spacing: 0.03em;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 0 1 auto;
  }

  .footer-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: center;
  }

  .site-credit {
    position: fixed;
    left: 14px;
    bottom: 10px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: #7d776f;
    z-index: 20;
  }
  .site-credit a {
    color: #9c958b;
    text-decoration: underline;
    text-underline-offset: 3px;
  }
  .site-credit a:hover { color: var(--ui-text); }
  .link-btn {
    background: none;
    border: none;
    padding: 0;
    color: #9c958b;
    font-size: 11px;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 3px;
  }
  .link-btn:hover { color: var(--ui-text); background: none; border: none; }

  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(14, 13, 12, 0.76);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 16px;
  }
  .modal-backdrop[hidden] { display: none; }
  .modal-card {
    background: var(--ui-bg);
    border: 1px solid var(--ui-border);
    color: var(--ui-text);
    max-width: 520px;
    width: 100%;
    padding: 18px 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.65);
    font-size: 12px;
    line-height: 1.6;
  }
  .modal-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--ui-border);
    color: #fff;
  }
  .modal-body p { margin-bottom: 8px; color: #b9b2a6; }
  .modal-body strong { color: var(--ui-text); font-weight: normal; }
  .modal-body a.info-link { color: #9c958b; text-decoration: underline; text-underline-offset: 3px; }
  .modal-body a.info-link:hover { color: var(--ui-text); }
  .shortcuts {
    display: inline-grid;
    grid-template-columns: auto auto;
    column-gap: 14px;
    row-gap: 2px;
    margin-bottom: 8px;
  }
  .shortcuts .key { justify-self: start; color: var(--ui-text); }
  .shortcuts .desc { justify-self: end; color: #b9b2a6; }

  /* Genuinely tiny screens only: stack the side controls around the stage. */
  @media (max-width: 640px) {
    body { padding: 8px 10px; gap: 8px; }
    .content {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 8px;
      width: 100%;
    }
    .modetabs {
      flex-direction: row;
      justify-content: center;
      justify-self: auto;
    }
    .movenavs {
      flex-direction: row;
      justify-content: center;
      justify-self: auto;
    }
    .stage {
      width: min(calc(100vw - 20px), calc((100vh - 190px) * var(--stage-ratio))) !important;
      max-width: 100%;
    }
    .footer { flex-wrap: wrap; gap: 8px; }
    .site-credit { position: static; margin-top: 2px; }
  }
