:root {
  --bg: #eef0ed;
  --surface: #ffffff;
  --surface-soft: #f7f6f1;
  --ink: #171a1c;
  --muted: #5f6768;
  --line: #ccd1ce;
  --accent: #17665a;
  --accent-hover: #0e5047;
  --danger: #a23d2a;
  --input-bg: #ffffff;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #111514;
  --surface: #1b2220;
  --surface-soft: #222a27;
  --ink: #eef3ef;
  --muted: #a7b3ad;
  --line: #3c4742;
  --accent: #4fb7a5;
  --accent-hover: #63c8b7;
  --danger: #e06d57;
  --input-bg: #151a18;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #111514;
    --surface: #1b2220;
    --surface-soft: #222a27;
    --ink: #eef3ef;
    --muted: #a7b3ad;
    --line: #3c4742;
    --accent: #4fb7a5;
    --accent-hover: #63c8b7;
    --danger: #e06d57;
    --input-bg: #151a18;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select,
textarea { font: inherit; }

button,
.file-button {
  min-height: 36px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 7px 11px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  white-space: nowrap;
}

button:hover:not(:disabled),
.file-button:hover:not(.disabled) { background: var(--accent-hover); }

button:disabled,
.file-button.disabled {
  opacity: .45;
  cursor: not-allowed;
  pointer-events: none;
}

button.quiet {
  min-height: 28px;
  padding: 3px 9px;
  background: transparent;
  color: var(--accent);
}

button.danger {
  border-color: var(--danger);
  background: transparent;
  color: var(--danger);
}

button.danger:hover:not(:disabled) {
  background: var(--danger);
  color: white;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 18px;
  min-height: 68px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-soft);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

h1,
h2,
p { margin: 0; }

h1 { font-size: 22px; }
h2 { font-size: 14px; }

.brand span {
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: thin;
}

.menu-toggle { display: none; }

.mobile-menu-backdrop,
.orientation-warning { display: none; }

.theme-control select {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 9px;
  background: var(--input-bg);
  color: var(--ink);
}

dialog {
  width: min(460px, calc(100vw - 24px));
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 18px 50px rgb(0 0 0 / 20%);
}

dialog::backdrop { background: rgb(20 28 29 / 45%); }

#settings-form {
  display: grid;
  gap: 16px;
  padding: 18px;
}

#help-dialog {
  width: min(520px, calc(100vw - 24px));
}

.dialog-content {
  display: grid;
  gap: 16px;
  padding: 18px;
}

.help-section {
  display: grid;
  gap: 8px;
}

.help-section h3 {
  margin: 0;
  font-size: 14px;
}

.help-section ul {
  margin: 0;
  padding-left: 20px;
}

.help-section li + li { margin-top: 5px; }

.help-section a {
  color: var(--accent);
  font-weight: 650;
}

#settings-form > label:not(.check-row),
.settings-field {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.field-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.field-heading a,
.field-help a {
  color: var(--accent);
  font-weight: 650;
}

.field-help {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 400;
  line-height: 1.45;
}

#settings-form input:not([type="checkbox"]),
#settings-form select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: var(--input-bg);
  color: var(--ink);
  font-weight: 400;
}

#settings-form input[readonly] { background: var(--surface-soft); }

.dialog-heading,
.dialog-actions,
.check-row {
  display: flex;
  align-items: center;
  gap: 9px;
}

.dialog-heading,
.dialog-actions { justify-content: space-between; }
.dialog-heading h2 { font-size: 17px; }
.dialog-actions { justify-content: flex-end; }

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 12px;
  padding: 12px;
  overflow: hidden;
}

.play-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.story {
  flex: 1;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 18px;
  background: var(--surface);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  scrollbar-gutter: stable;
}

.story.empty,
.map.empty { color: var(--muted); }

.status-bar {
  display: block;
  min-height: 34px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 10px;
  background: var(--surface-soft);
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-bar.error {
  border-color: var(--danger);
  color: var(--danger);
}

.status-bar.working {
  border-color: var(--accent);
  color: var(--accent);
}

.command-form {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.command-form input {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 11px;
  background: var(--input-bg);
  color: var(--ink);
}

.voice-button {
  width: 66px;
  min-width: 66px;
}

.voice-button.recording {
  border-color: var(--danger);
  background: var(--danger);
  color: white;
}

.side-pane {
  display: grid;
  grid-template-rows: auto auto minmax(190px, 1fr) minmax(90px, .55fr);
  gap: 10px;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.panel {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 11px 12px;
  background: var(--surface);
}

.panel h2 { margin-bottom: 8px; }

dl {
  display: grid;
  gap: 5px;
  margin: 0;
}

dl div {
  display: grid;
  grid-template-columns: minmax(86px, 34%) minmax(0, 1fr);
  gap: 10px;
}

dt {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

dd { margin: 0; overflow-wrap: anywhere; }

#inventory {
  max-height: 84px;
  overflow: auto;
  margin: 0;
  padding-left: 20px;
}

.map-panel {
  display: flex;
  flex-direction: column;
}

.debug-panel { position: relative; }

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.map {
  flex: 1;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface-soft);
}

.map.empty { display: grid; place-items: center; }

summary { cursor: pointer; font-weight: 650; }

#debug-scroll {
  position: absolute;
  inset: 40px 12px 11px;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: scroll;
  margin: 0;
  padding: 0 6px 10px 0;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
}

#debug-scroll::-webkit-scrollbar { width: 10px; }
#debug-scroll::-webkit-scrollbar-track { background: var(--surface-soft); }
#debug-scroll::-webkit-scrollbar-thumb {
  border: 2px solid var(--surface-soft);
  border-radius: 5px;
  background: #9da7a3;
}

#debug-history {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 12px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.busy { cursor: progress; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 820px) {
  .topbar {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 8px 10px;
  }

  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .toolbar {
    position: fixed;
    z-index: 20;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(280px, 82vw);
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 9px;
    border-left: 1px solid var(--line);
    padding: 12px;
    background: var(--surface);
    box-shadow: -12px 0 30px rgb(0 0 0 / 18%);
    overflow-y: auto;
    transform: translateX(100%);
    transition: transform 160ms ease;
  }

  .toolbar button,
  .toolbar .file-button,
  .toolbar select {
    width: 100%;
  }

  .menu-open .toolbar { transform: translateX(0); }

  .mobile-menu-backdrop {
    position: fixed;
    z-index: 19;
    inset: 0;
    display: block;
    background: rgb(0 0 0 / 28%);
  }

  .mobile-menu-backdrop[hidden] { display: none; }

  .theme-control {
    display: grid;
    gap: 4px;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(240px, 56%) minmax(0, 44%);
    padding: 8px;
  }

  .side-pane {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: minmax(92px, auto) minmax(150px, 1fr);
  }

  .map-panel { grid-column: 1; }
  .debug-panel { grid-column: 2; }
  .debug-panel:not([open]) { align-self: start; }
}

@media (max-width: 820px) and (orientation: portrait) {
  .app { visibility: hidden; }

  .orientation-warning {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-content: center;
    gap: 8px;
    padding: 28px;
    background: var(--bg);
    color: var(--ink);
    text-align: center;
  }

  .orientation-warning strong { font-size: 20px; }
  .orientation-warning span { color: var(--muted); }
}
