:root {
  color-scheme: dark;
  --bg: #11151c;
  --panel: #181f29;
  --panel-2: #222b37;
  --line: #344254;
  --text: #f0f3f8;
  --muted: #afbbca;
  --accent: #ffbe70;
  --accent-2: #92c6ff;
  --danger: #ff8793;
  --ink: var(--text);
  --dim: var(--muted);
  --good: #78dfa8;
  --bad: #ff8793;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --shadow: 0 12px 35px #05091020;
  font: 16px/1.42 Inter, "Segoe UI", system-ui, sans-serif;
}

html, body { width: auto; min-height: 100%; height: auto; overflow: visible; }
body { display: block; }
[hidden] { display: none !important; }

a { color: inherit; }
button, .file-button, select, input[type="text"], input[type="number"] {
  min-height: 36px;
  height: auto;
  border-radius: 7px;
  font-size: .875rem;
  padding: 6px 10px;
}
button, .file-button { font-weight: 600; text-decoration: none; white-space: nowrap; }
button:hover, .file-button:hover, select:hover { background: #2c3746; border-color: #75869b; }
button.primary, .file-button.primary {
  background: var(--accent);
  border-color: #e5aa66;
  color: #242019;
}
button.primary:hover, .file-button.primary:hover { background: #ffd097; }
button:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible, canvas:focus-visible,
.file-button:focus-within { outline: 2px solid var(--accent-2); outline-offset: 3px; }

input[type="range"], input[type="checkbox"] { accent-color: var(--accent); }
.file-button { position: relative; overflow: hidden; }
.file-button input[type="file"] { display: block; opacity: 0; inset: 0; position: absolute; width: 100%; cursor: pointer; }
select {
  appearance: none;
  padding-right: 31px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='m1 1 5 5 5-5' fill='none' stroke='%23afbbca' stroke-width='1.7'/%3E%3C/svg%3E");
  background-size: 11px 8px;
  background-position: right 10px center;
  background-repeat: no-repeat;
}
select:hover { background-color: #2c3746; }

.app-header {
  position: sticky;
  z-index: 3;
  top: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 24px;
  min-height: 68px;
  padding: 0 clamp(14px, 2.5vw, 40px);
  border-bottom: 1px solid var(--line);
  background: #141b24;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  justify-self: start;
  text-decoration: none;
  white-space: nowrap;
  color: var(--text);
  font-size: 1.13rem;
  font-weight: 750;
  letter-spacing: -.023em;
}
.brand strong { color: var(--accent); font-weight: 750; }
.brand-mark {
  width: 32px;
  height: 32px;
  background: #392c20;
  border: 1px solid #765234;
  border-radius: 9px;
  display: block;
  position: relative;
}
.brand-mark::before, .brand-mark::after {
  content: "";
  position: absolute;
  top: 14px;
  left: 5px;
  width: 20px;
  height: 2px;
  background: #f3b46a;
  transform: rotate(-43deg);
}
.brand-mark::after { transform: rotate(43deg); }
.brand-mark i { position: absolute; background: #ffcb8b; width: 5px; height: 5px; border-radius: 50%; }
.brand-mark i:nth-child(1) { top: 7px; left: 6px; }
.brand-mark i:nth-child(2) { top: 7px; right: 6px; }
.brand-mark i:nth-child(3) { bottom: 7px; left: 6px; }
.brand-mark i:nth-child(4) { bottom: 7px; right: 6px; }
.app-header nav { display: flex; height: 67px; align-items: stretch; gap: 16px; }
.app-header nav a {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0 10px;
  text-decoration: none;
  font-size: .92rem;
  font-weight: 650;
  color: var(--muted);
  white-space: nowrap;
}
.app-header nav a:hover { color: var(--text); }
.app-header nav a[aria-current="page"] { color: var(--accent); }
.app-header nav a[aria-current="page"]::after {
  content: "";
  height: 3px;
  background: var(--accent);
  border-radius: 4px 4px 0 0;
  position: absolute;
  bottom: -1px;
  inset-inline: 9px;
}
.workspace-profile { justify-self: end; display: flex; align-items: center; gap: 10px; min-width: 0; }
.workspace-profile label { color: var(--muted); font-size: .875rem; }
.workspace-profile select { min-width: 145px; max-width: 220px; }

#workspace { width: min(1720px, 100%); margin-inline: auto; padding: 24px clamp(14px, 2.2vw, 32px) 24px; }
.screen { min-width: 0; }
.screen h1 { color: var(--text); font-size: clamp(1.55rem, 2.4vw, 2.1rem); letter-spacing: -.04em; font-weight: 770; }
.atlas-toolbar, .screen-heading {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: end;
  gap: 12px 24px;
  margin-bottom: 18px;
  padding: 0;
  border: 0;
  background: transparent;
}
.screen-heading p, #status { margin: 7px 0 0; font-size: .9rem; color: var(--muted); }
#status { white-space: normal; line-height: 1.38; max-width: 660px; min-height: 1.5em; }
#appStatus { color: var(--accent); margin: 12px 0 0; font-size: .875rem; }
#appStatus:empty { display: none; }
.toolbar { gap: 8px; justify-content: flex-start; }

.atlas-grid {
  padding: 0;
  gap: 13px;
  grid-template-columns: 225px minmax(350px, 1fr) minmax(280px, 344px);
  align-items: stretch;
  min-height: 590px;
}
.panel, .stage, .parts, .motion-stage, #stage {
  border-color: var(--line);
  border-radius: 12px;
  background: var(--panel);
}
.panel-head, .stage-head, .parts-head { min-height: 49px; padding: 8px 12px; background: #1b2430; }
.panel-head h2, .stage-head h2, .parts-head h2 { font-size: .95rem; }
.panel-body { padding: 13px; overflow: visible; }
.field, .field-row { min-width: 0; }
.field label, .field-label, .stage-tools, .check-row { font-size: .875rem; }
.field-row { gap: 8px; }
.field input, .field select { max-width: 100%; min-width: 0; }
.field input[type="color"] { width: 100%; }
.value-line, #selRole { color: var(--muted); font-size: .875rem; }
.atlas-grid .stage { min-width: 0; display: flex; flex-direction: column; }
.atlas-grid .canvas-wrap { flex: 1; height: 100%; min-height: 490px; background: #202936; }
.atlas-grid .preview-wrap {
  background: radial-gradient(ellipse at 50% 43%, #303e51 0%, #222d3c 50%, #151e2a 100%);
  height: 243px;
  aspect-ratio: auto;
}
.canvas-wrap canvas, .preview-wrap canvas, #motionCanvas { display: block; width: 100%; height: 100%; }
.inspector > .check-row { margin-bottom: 12px; }
.parts { margin: 14px 0 0; }
.parts-table-wrap { max-height: 360px; overflow: auto; }
th { font-size: .8rem; }
td { font-size: .875rem; }
.parts tr.selected td { background: #352d27; color: #ffdeba; }
.parts td input[type="number"] { max-width: 82px; min-height: 28px; }

.motion-grid {
  display: grid;
  grid-template-columns: minmax(0,1fr) minmax(242px,300px);
  gap: 14px;
  align-items: start;
}
.motion-stage { overflow: hidden; min-width: 0; }
.motion-surface {
  position: relative;
  height: clamp(330px, 57vh, 640px);
  background: radial-gradient(ellipse at 50% 64%, #26394d, #141d29 70%);
}
.motion-properties .check-row { margin-bottom: 13px; }
.motion-properties .field { margin-top: 19px; font-size: .875rem; }
.transport {
  padding: 13px 15px;
  display: grid;
  grid-template-columns: auto minmax(80px,1fr) auto;
  gap: 14px;
  align-items: center;
  border-top: 1px solid var(--line);
}
.transport-buttons { display: flex; gap: 7px; }
.transport output, .readouts dd { font: .83rem var(--mono); font-variant-numeric: tabular-nums; }
.readouts { border-top: 1px solid var(--line); padding-top: 8px; margin-bottom: 0; }
.readouts div { display: flex; justify-content: space-between; gap: 12px; padding-block: 7px; }
.readouts dt { color: var(--muted); font-size: .875rem; }
.readouts dd { margin: 0; }

#app {
  display: grid;
  grid-template-columns: minmax(0,1fr) minmax(260px,310px);
  gap: 14px;
  align-items: start;
}
#stage { position: relative; overflow: hidden; min-width: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); }
#skipCheer { position: absolute; top: 12px; right: 12px; background: #17232ee8; color: #f6e9d1; }
#cv { display: block; width: 100%; height: auto; aspect-ratio: 16 / 9; }
.tideworks-cover {
  min-height: clamp(395px, 72svh, 765px);
  display: grid;
  align-items: end;
  border: 1px solid #79614e;
  border-radius: 16px;
  overflow: hidden;
  background: linear-gradient(0deg, #111c29f7 4%, #152331b0 39%, transparent 80%),
    #344957 url('./assets/tideworks/ink/lock.webp') center 55% / cover no-repeat;
  color: #fff0d5;
}
.tideworks-cover-copy { max-width: 37rem; padding: clamp(22px, 6vw, 84px); }
.tideworks-cover h1 { font: 600 clamp(3rem, 8vw, 7rem)/0.95 Georgia, serif; margin: 0 0 .24em; }
.tideworks-cover p { margin: 0 0 22px; max-width: 32em; }
.tideworks-cover .toolbar { flex-wrap: wrap; }
/* In Levels the game owns the viewport: the page heading goes, the stage
   bar is slim, and the shell measures the room left under it each frame
   (--levels-fit) so the canvas and its outcome box end above the fold. */
#playtestScreen[data-mode="levels"] .screen-heading { display: none; }
#playtestScreen[data-mode="levels"] #levelPanel { gap: 6px 14px; padding: 8px 12px; margin-bottom: 8px; }
#playtestScreen[data-mode="levels"] #app { grid-template-columns: minmax(0,1fr); }
#playtestScreen[data-mode="levels"] #cv { max-height: var(--levels-fit, max(300px, calc(100svh - 227px))); }
#playtestScreen[data-mode="levels"] #panel { grid-row: auto; grid-template-columns: repeat(auto-fit, minmax(min(245px,100%),1fr)); }
#playtestScreen[data-mode="levels"] #panel > div + div { border-top: 0; padding-top: 0; }
#playtestScreen[data-mode="levels"] #levelOutcome[data-result="failed"] {
  margin-inline: 0;
  padding: 8px 12px;
  background: #1b2531f5;
  border-color: #b6866b;
}
#playtestScreen[data-mode="levels"] #levelOutcome[data-result="clear"] {
  animation: tideworks-clear .45s ease-out both;
}
@keyframes tideworks-clear {
  from { opacity: 0; transform: translateY(9px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  #playtestScreen[data-mode="levels"] #levelOutcome[data-result="clear"] { animation: none; }
}
#districtMap {
  width: min(640px, calc(100% - 24px));
  max-height: calc(100dvh - 24px);
  box-sizing: border-box;
  padding: 0 16px 16px;
  color: #f6e9d1;
  background: #213436;
  border: 1px solid #718477;
  border-radius: 9px;
  box-shadow: var(--shadow);
}
#districtMap::backdrop { background: #101820a8; }
.district-heading {
  position: sticky; top: 0; z-index: 1; background: #213436;
  display: flex; justify-content: space-between; align-items: center; padding-block: 12px; gap: 12px;
}
.district-heading h2 { margin: 0; font-size: 1rem; }
#districtStages { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%,205px),1fr)); gap: 10px; margin-top: 10px; }
#districtStages section { display: flex; flex-wrap: wrap; gap: 5px; align-content: start; }
#districtStages h2 { width: 100%; margin: 0; color: #f4d396; font-size: .9rem; }
#districtStages button { font-size: .8rem; min-height: 33px; white-space: normal; }
#districtStages button[aria-current="step"] { border-color: var(--accent); color: var(--accent); }
#districtStages button small { display: block; margin-top: 1px; font-size: .68rem; font-weight: 500; color: #cdb98f; letter-spacing: .02em; }
#districtStages button small[data-token="true"] { color: #f4d396; }
#levelSummary { margin: -2px 0 10px; font-size: .84rem; color: #d8c7a3; }
@media (max-width: 700px) {
  #playtestScreen[data-mode="levels"] #stage { margin-inline: calc(0px - clamp(14px, 2.2vw, 32px)); border-radius: 0; }
  #playtestScreen[data-mode="levels"] #cv { aspect-ratio: 4 / 3; }
  #playtestScreen[data-mode="levels"] #levelObjective { order: 2; font-size: .84rem; }
  #playtestScreen[data-mode="levels"] #playHint { font-size: .72rem; top: 6px; }
  #levelPanel button { padding-inline: 7px; }
}
@media (pointer: coarse) and (max-width: 1024px) {
  #playtestScreen[data-mode="levels"] #levelJoin { display: none; }
  #playtestScreen[data-mode="levels"] .touch-controls { display: grid; grid-template-columns: repeat(7,minmax(0,1fr)); gap: 4px; }
  #playtestScreen[data-mode="levels"] .touch-controls button { min-width: 0; padding-inline: 1px; font-size: .72rem; }
}
#playHint {
  position: absolute;
  inset: 12px 12px auto;
  box-sizing: border-box;
  width: fit-content;
  max-width: min(27rem, calc(100% - 24px));
  margin: 0 auto;
  padding: 0 5px;
  border: 10px solid transparent;
  border-image: url('./assets/tideworks/fit/hint-enamel.webp') 27% 12% fill / 10px 14px;
  background: #f3e3c3 padding-box;
  color: #213a38;
  font-size: .875rem;
  font-weight: 650;
  line-height: 1.3;
  text-align: center;
  pointer-events: none;
}
#playHint[data-tone="caution"] {
  border-image-source: url('./assets/tideworks/fit/hint-slate.webp');
  background-color: #234950;
  color: #fff0ce;
}
#playHint:empty { opacity: 0; }
#levelPanel {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 9px 18px;
  padding: 12px 16px;
  margin-bottom: 12px;
  border: 1px solid #44645f;
  border-radius: 8px;
  background: linear-gradient(110deg, #263e3b, #1b292d 68%);
  box-shadow: inset 0 1px 0 #d6b17e26;
}
#levelSelect { width: auto; min-width: 10.5rem; max-width: 100%; border-color: #718477; color: #fae6bd; }
#levelObjective {
  flex: 1 1 280px;
  margin: 0;
  color: #f6e9d1;
  font-size: .92rem;
  line-height: 1.45;
}
#levelOutcome {
  position: absolute;
  inset: auto 10px 10px;
  margin-inline: auto;
  width: fit-content;
  max-width: calc(100% - 20px);
  padding: 13px 18px;
  border: 1px solid #b59768;
  border-radius: 8px;
  background: linear-gradient(150deg, #264140fa, #182c32fa);
  color: #f8e8cc;
  box-shadow: var(--shadow);
  text-align: center;
}
#levelOutcomeMessage { margin: 0 0 8px; }
#yardShortcuts { display: flex; flex-wrap: wrap; gap: 2px 8px; margin-top: 4px; }
#qa, #hud { position: absolute; pointer-events: none; font: .78rem/1.46 var(--mono); }
#hud { left: 12px; top: 12px; white-space: pre; text-shadow: 0 2px 3px #000; }
#qa { left: 10px; bottom: 8px; padding: 5px 8px; background: #080b10d9; border-radius: 6px; }
.ok { color: var(--good); }
.err { color: var(--bad); }
#panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: grid; gap: 12px; }
#panel > div + div { padding-top: 10px; border-top: 1px solid var(--line); }
#panel h2 { margin: 0 0 7px; font-size: .92rem; }
#panel .row { display: flex; gap: 6px; flex-wrap: wrap; }
#panel .wrow { display: flex; align-items: center; gap: 8px; margin: 5px 0; font-size: .875rem; }
#panel .wrow span:first-child { min-width: 42px; }
#panel .wrow select, #panel input[type="range"] { width: 100%; min-width: 0; }
#panel .keys { font-size: .875rem; line-height: 1.9; color: var(--muted); }
#panel .keys b { background: var(--panel-2); color: var(--text); font-weight: 650; border-radius: 4px; padding: 1px 5px; }
#panel .chk { display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: .875rem; }
#panel .small { color: var(--muted); font-size: .8rem; }
#panel .toggled { background: #61432d; border-color: #ad774b; color: #ffdbb4; }

@media (max-width: 1160px) {
  .atlas-grid { grid-template-columns: 195px minmax(0,1fr) 270px; }
  .atlas-grid .stage-head { flex-wrap: wrap; }
  .check-row { white-space: normal; }
}
@media (max-width: 950px) {
  .app-header { gap: 10px; grid-template-columns: 1fr auto; }
  .app-header nav { grid-area: 2 / 1 / 3 / -1; justify-content: start; height: 43px; }
  .brand, .workspace-profile { min-height: 52px; }
  .atlas-grid { grid-template-columns: minmax(0,1fr) 285px; }
  .atlas-grid > aside:first-child { grid-row: 2; grid-column: 1 / -1; }
  .atlas-grid > aside:first-child .panel-body { display: grid; grid-template-columns: repeat(3,1fr); align-items: start; gap: 10px; }
  .atlas-grid .canvas-wrap { min-height: 480px; }
}
@media (max-width: 700px) {
  .motion-grid, #app, .atlas-grid { grid-template-columns: minmax(0,1fr); }
  .atlas-grid > aside:first-child { grid-row: 3; }
  .atlas-grid > aside:first-child .panel-body { grid-template-columns: repeat(2,1fr); }
  .atlas-grid .canvas-wrap { min-height: 390px; }
  .transport { grid-template-columns: 1fr auto; gap: 8px; }
  .transport-buttons { grid-column: 1 / -1; }
  .workspace-profile label { display: none; }
  .workspace-profile select { min-width: 0; max-width: 145px; }
  .app-header nav { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 4px; }
  .app-header nav a { font-size: .83rem; padding-inline: 5px; white-space: normal; text-align: center; }
}

.atlas-toolbar .title { min-width: 0; }
.atlas-toolbar .toolbar select { width: auto; }
.atlas-grid { min-height: 0; grid-template-columns: 239px minmax(0,1fr) minmax(286px,344px); }
.atlas-grid .canvas-wrap { height: auto; min-height: 0; }
.atlas-grid .canvas-wrap canvas { position: absolute; inset: 0; }
.atlas-grid .stage { min-height: clamp(510px, 66vh, 710px); }
.atlas-grid .stage-tools { flex-wrap: wrap; }
.atlas-grid .stage-tools button { min-width: 30px; padding-inline: 8px; }
.atlas-grid .panel-body { padding: 12px; }
#selRole { overflow-wrap: anywhere; }
.advanced { margin: 13px 0; border-block-start: 1px solid var(--line); padding-top: 9px; }
.advanced summary { cursor: pointer; color: var(--text); font-size: .875rem; }
.advanced[open] summary { margin-bottom: 10px; }
.cleanup-btn { width: 100%; margin-bottom: 9px; }
.button-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.button-grid button { font-size: .81rem; padding-inline: 4px; }
.stage-tools { gap: 8px; }
.motion-mode { display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: .875rem; }
.motion-mode select { width: auto; min-width: 130px; }
@media (max-width: 1160px) {
  .atlas-grid { grid-template-columns: 210px minmax(0,1fr) 272px; }
}
@media (max-width: 950px) {
  .atlas-grid { grid-template-columns: minmax(0,1fr) 285px; }
  .atlas-grid .canvas-wrap { min-height: 430px; }
  .atlas-grid .stage { min-height: 0; }
}
@media (max-width: 700px) {
  .atlas-grid { grid-template-columns: minmax(0,1fr); }
  .atlas-grid .canvas-wrap { min-height: 380px; }
}

.reference-tools .file-button { width: 100%; margin-block: 3px 9px; }
.reference-tools .check-row { margin: 5px 0; }
.reference-tools p { color: var(--muted); font-size: .875rem; margin-bottom: 0; }
.reference-tools p:empty { display: none; }

.touch-controls { display: none; }
@media (pointer: coarse) {
  .touch-controls { grid-row: 2; display: flex; flex-wrap: wrap; gap: 8px; }
  .touch-controls button { flex: 1; min-width: 52px; min-height: 44px; touch-action: none; }
  #panel { grid-row: span 2; }
  @media (max-width: 700px) { #panel { grid-row: 3; } }
}

@media (orientation: landscape) and (max-height: 480px) {
  body:has(#playtestScreen[data-mode="levels"]:not([hidden])) .app-header {
    min-height: 42px; grid-template-columns: auto 1fr; gap: 8px; padding-inline: 10px;
  }
  body:has(#playtestScreen[data-mode="levels"]:not([hidden])) .app-header nav {
    grid-area: auto; display: flex; justify-content: end; height: 41px;
  }
  body:has(#playtestScreen[data-mode="levels"]:not([hidden])) .workspace-profile { display: none; }
  body:has(#playtestScreen[data-mode="levels"]:not([hidden])) #workspace { padding: 5px 12px; }
  #playtestScreen[data-mode="levels"] #levelPanel { padding: 4px 6px; margin-bottom: 4px; }
  #playtestScreen[data-mode="levels"] #levelObjective { display: none; }
  #playtestScreen[data-mode="levels"] #levelPanel :is(button, select) { min-height: 30px; padding-block: 3px; }
  #playtestScreen[data-mode="levels"] #app { gap: 8px; }
  #playtestScreen[data-mode="levels"] #stage { margin-inline: 0; }
}
