/* V1.6 — continuous 30-second grayscale wave, responsive layout */
:root {
  color-scheme: light;
  --page: #e5e5e5;
  --text: #222222;
  --muted: #444444;
  --border: #888888;
  --header-bg: repeating-linear-gradient(90deg,
    #373737 0%,
    #383838 3.125%,
    #3d3d3d 6.25%,
    #434343 9.375%,
    #4a4a4a 12.5%,
    #515151 15.625%,
    #575757 18.75%,
    #5c5c5c 21.875%,
    #5d5d5d 25%,
    #5c5c5c 28.125%,
    #575757 31.25%,
    #515151 34.375%,
    #4a4a4a 37.5%,
    #434343 40.625%,
    #3d3d3d 43.75%,
    #383838 46.875%,
    #373737 50%);
  --logo-bg: linear-gradient(135deg, #111111, #333333);
  --nav-bg: linear-gradient(180deg, #777777, #aaaaaa);
  --main-bg: linear-gradient(135deg, #ffffff, #dddddd);
  --aside-bg: linear-gradient(180deg, #cccccc, #999999);
  --footer-bg: linear-gradient(135deg, #555555, #222222);
  --button-bg: linear-gradient(180deg, #ffffff, #cccccc);
  --button-text: #222222;
  --active-bg: linear-gradient(180deg, #555555, #333333);
  --active-text: #ffffff;
  --card-bg: linear-gradient(135deg, #ffffff, #dddddd);
  --focus: #111111;
  --scroll-thumb: #444444;
  --scroll-track: #bbbbbb;
  --nav-width: 160px;
  --aside-width: 280px;
  --grid-gap: 12px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #161616;
  --text: #eeeeee;
  --muted: #cccccc;
  --border: #666666;
  --header-bg: repeating-linear-gradient(90deg,
    #1c1c1c 0%,
    #1d1d1d 3.125%,
    #202020 6.25%,
    #242424 9.375%,
    #282828 12.5%,
    #2d2d2d 15.625%,
    #313131 18.75%,
    #343434 21.875%,
    #353535 25%,
    #343434 28.125%,
    #313131 31.25%,
    #2d2d2d 34.375%,
    #282828 37.5%,
    #242424 40.625%,
    #202020 43.75%,
    #1d1d1d 46.875%,
    #1c1c1c 50%);
  --logo-bg: linear-gradient(135deg, #101010, #252525);
  --nav-bg: linear-gradient(180deg, #262626, #383838);
  --main-bg: linear-gradient(135deg, #252525, #191919);
  --aside-bg: linear-gradient(180deg, #303030, #232323);
  --footer-bg: linear-gradient(135deg, #292929, #111111);
  --button-bg: linear-gradient(180deg, #444444, #303030);
  --button-text: #eeeeee;
  --active-bg: linear-gradient(180deg, #eeeeee, #bcbcbc);
  --active-text: #181818;
  --card-bg: linear-gradient(135deg, #404040, #303030);
  --focus: #ffffff;
  --scroll-thumb: #999999;
  --scroll-track: #292929;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font-family: Arial, sans-serif;
}

/* Desktop: each middle panel scrolls independently. */
.grid-container {
  display: grid;
  grid-template-columns: var(--nav-width) minmax(0, 1fr) var(--aside-width);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "header header header"
    "nav    main   aside"
    "footer footer footer";
  gap: var(--grid-gap);
  padding: 12px;
  height: 100vh;
  height: 100dvh;
}
.grid-container > * {
  min-width: 0;
  min-height: 0;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 14px;
}

/* Reuse the outer grid tracks so Donate begins at the left edge of Aside. */
.header {
  grid-area: header;
  background: var(--header-bg);
  /* Two identical wave periods in the image; one period per header width. */
  background-size: 200% 100%;
  background-position: 100% 50%;
  animation: header-gradient 15s linear infinite;
  display: grid;
  grid-template-columns: var(--nav-width) minmax(0, 1fr) var(--aside-width);
  column-gap: var(--grid-gap);
  align-items: center;
  padding: 18px 0;
}
/* Move right by one full wavelength. The two endpoints are visually identical. */
@keyframes header-gradient {
  from { background-position: 100% 50%; }
  to   { background-position: 0% 50%; }
}

/* The visitor can disable motion in Settings. Respect OS preferences too. */
:root[data-header-motion="off"] .header {
  animation: none;
}
@media (prefers-reduced-motion: reduce) {
  .header { animation: none; }
}

@supports (grid-template-columns: subgrid) {
  .header { grid-template-columns: subgrid; }
}
.logo {
  grid-column: 1 / -1;
  grid-row: 1;
  justify-self: center;
  max-width: calc(100% - 24px);
  background: var(--logo-bg);
  color: #ffffff;
  border: 1px solid #999999;
  border-radius: 12px;
  padding: 20px 26px;
  text-align: center;
  overflow-wrap: anywhere;
}
.logo h1 {
  margin: 0;
  font-size: clamp(20px, 2.5vw, 28px);
  font-weight: bold;
}
.logo p {
  margin: 14px 0 0;
  color: #dddddd;
  font-size: 14px;
}
.header-actions {
  grid-column: 3;
  grid-row: 1;
  justify-self: start;
  display: flex;
  gap: 8px;
  align-items: center;
}

/* Shared monochrome controls */
.nav button, .simulation-controls button, .header-actions button, .donate-dialog button {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--button-bg);
  color: var(--button-text);
  font: bold 12px/1.3 Arial, sans-serif;
  cursor: pointer;
}
.header-actions button {
  min-width: 72px;
  min-height: 34px;
  padding: 6px 12px;
}
.nav button:hover, .nav button.active,
.header-actions button:hover,
.simulation-controls button:hover:not(:disabled),
.donate-dialog button:hover {
  background: var(--active-bg);
  color: var(--active-text);
}
button:focus-visible, input:focus-visible, a:focus-visible, .main:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.header-actions button:focus-visible { outline-color: #ffffff; }

/* Smaller buttons on desktop: width and height reduced, text stays legible. */
.nav {
  grid-area: nav;
  background: var(--nav-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  padding: 12px 10px;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
}
.nav button {
  flex-shrink: 0;
  width: 124px;
  max-width: 100%;
  min-height: 34px;
  padding: 6px 7px;
  overflow-wrap: anywhere;
}

.main {
  grid-area: main;
  background: var(--main-bg);
  color: var(--text);
  overflow: auto;
  line-height: 1.6;
  overflow-wrap: anywhere;
}
.main h2 { margin: 0 0 8px; font-size: 24px; color: var(--text); }
.main p { margin: 0 0 16px; }
.main pre {
  background: #222222;
  color: #eeeeee;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 9px;
  overflow-x: auto;
  font: 14px/1.6 Consolas, monospace;
  tab-size: 4;
  white-space: pre;
}
.main input[type="range"] { max-width: 100%; }
.motion-setting {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  width: fit-content;
  min-height: 44px;
}
.motion-setting input { width: 18px; height: 18px; accent-color: var(--focus); }
.motion-note { font-size: 13px; color: var(--muted); }

.aside {
  grid-area: aside;
  background: var(--aside-bg);
  color: var(--text);
  overflow: auto;
}
.aside h2 { margin: 0 0 8px; font-size: 18px; }
.aside h3 { margin: 22px 0 10px; font-size: 15px; }
.news-intro { margin: 0 0 16px; font-size: 12px; color: var(--muted); }
.news-list { display: flex; flex-direction: column; gap: 12px; }
.news-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--card-bg);
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}
.news-meta, .news-action { font-size: 12px; color: var(--muted); }
.news-card:hover, .news-card:focus-visible {
  background: var(--active-bg);
  color: var(--active-text);
}
.news-card:hover .news-meta, .news-card:hover .news-action,
.news-card:focus-visible .news-meta, .news-card:focus-visible .news-action { color: inherit; }

.footer {
  grid-area: footer;
  background: var(--footer-bg);
  color: #ffffff;
  text-align: center;
  font-size: 14px;
  padding: 20px;
}
.nav, .main, .aside {
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) var(--scroll-track);
}

/* Simulation: unchanged JavaScript engine, themed controls. */
.simulation-controls { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0; }
.simulation-controls button { padding: 10px 16px; }
.simulation-controls button:disabled { opacity: 0.5; cursor: not-allowed; }
.simulation-screen {
  background: #222222;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: #eeeeee;
  padding: 18px;
  font-family: Consolas, monospace;
}
.simulation-track {
  height: 36px;
  position: relative;
  border: 1px solid #777777;
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 16px;
}
.simulation-object { position: absolute; top: 5px; left: 0; width: 24px; height: 24px; border-radius: 50%; background: #eeeeee; }

.donate-dialog {
  width: min(440px, calc(100% - 32px));
  max-height: calc(100% - 32px);
  overflow: auto;
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--main-bg);
  color: var(--text);
}
.donate-dialog::backdrop { background: #00000099; }
.donate-dialog h2 { margin-top: 0; }
.donate-dialog button { min-height: 44px; padding: 10px 20px; }

/* Medium widths: keep the logo centered and avoid overlapping the controls. */
@media (max-width: 1150px) {
  .header { row-gap: 12px; }
  .header-actions { grid-row: 2; }
}

/* Android / iOS / narrow browsers: stack the five regions. The document
   scrolls normally; only the topic menu keeps an independent scroll area. */
@media (max-width: 900px), (max-height: 480px) {
  .grid-container {
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 8px;
    gap: 8px;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    grid-template-areas: "header" "nav" "main" "aside" "footer";
  }
  .grid-container > * { padding: 12px; border-radius: 12px; }
  .header { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .logo { grid-column: 1; grid-row: 1; max-width: 100%; padding: 14px 16px; }
  .logo h1 { font-size: clamp(18px, 4.5vw, 26px); }
  .logo p { margin-top: 8px; font-size: 12px; }
  .header-actions { grid-column: 1; grid-row: 2; justify-self: center; flex-wrap: wrap; justify-content: center; }
  .header-actions button { min-height: 44px; min-width: 80px; }
  .nav {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(126px, 100%), 1fr));
    grid-auto-rows: minmax(44px, auto);
    align-content: start;
    align-items: stretch;
    gap: 7px;
    max-height: 174px;
    overflow-y: auto;
  }
  .nav button { width: 100%; min-height: 44px; padding: 6px 8px; }
  .main { min-height: 280px; overflow: visible; }
  .main h2 { font-size: 21px; }
  .aside { overflow: visible; }
  .simulation-controls button { min-height: 44px; }
}

/* Touch devices with a wider viewport still get usable targets. */
@media (pointer: coarse) {
  .nav button, .header-actions button, .simulation-controls button { min-height: 44px; }
}
