:root {
  color-scheme: dark;
  --bg: #10160e;
  --panel: #1b2418;
  --panel-2: #26331f;
  --cell: #172011;
  --ink: #f3f7e8;
  --muted: #a9b99b;
  --line: #3a482e;
  --green: #8fd06b;
  --gold: #e5bd55;
  --red: #d97755;
  --blue: #7fb7a5;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background:
    radial-gradient(circle at 18% 12%, rgba(143, 208, 107, 0.13), transparent 28%),
    linear-gradient(135deg, rgba(78, 112, 43, 0.18), transparent 34%),
    linear-gradient(315deg, rgba(127, 183, 165, 0.12), transparent 36%),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
textarea {
  font: inherit;
}

.dungeon-app {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(360px, 1fr) minmax(320px, 430px);
  gap: 16px;
  height: 100svh;
  min-height: 0;
  overflow: hidden;
  padding: 16px;
}

.left-panel,
.center-panel,
.right-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.center-panel {
  display: grid;
  grid-template-rows: minmax(0, auto) minmax(190px, 0.34fr);
}

.masthead,
.panel,
.editor-panel,
.dungeon-stage {
  background: rgba(27, 36, 24, 0.94);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.masthead,
.panel {
  padding: 14px;
}

.eyebrow,
.section-label {
  margin: 0 0 7px;
  color: var(--green);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3.4rem);
  line-height: 0.95;
  letter-spacing: 0;
}

.muted {
  color: var(--muted);
  line-height: 1.45;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stats div {
  padding: 10px;
  background: #11180d;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.stats span {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  margin-bottom: 5px;
}

.stats strong {
  font-size: 1.2rem;
}

.goal-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.goal-list li {
  display: grid;
  gap: 3px;
  padding: 9px 10px;
  background: #11180d;
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: 8px;
}

.goal-list .active {
  border-left-color: var(--gold);
}

.goal-list .met {
  border-left-color: var(--green);
}

.goal-list .failed {
  border-left-color: var(--red);
}

.rule-list {
  display: grid;
  gap: 8px;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.rule-card {
  padding: 10px;
  background: #11180d;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-align: center;
}

.rule-card strong {
  display: block;
  margin: 0;
}

.rule-card .muted {
  display: none;
}

.rule-card:disabled {
  color: #6f7b62;
}

.rule-card:disabled .rule-pattern,
.rule-card:disabled .muted {
  color: #6f7b62;
}

code,
pre,
textarea,
.rule-pattern,
.formula,
.cell-formula {
  font-family: "Cascadia Code", Consolas, monospace;
}

.rule-pattern {
  color: var(--gold);
}

button {
  min-height: 40px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--ink);
  border-radius: 8px;
  cursor: pointer;
}

button:disabled {
  color: #6f7b62;
  cursor: not-allowed;
}

.dungeon-stage {
  display: grid;
  place-items: center;
  min-height: 0;
  overflow: hidden;
  padding: 14px;
}

.dungeon-grid {
  --grid-gap: 6px;
  --grid-max: 620px;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: repeat(7, minmax(0, 1fr));
  gap: var(--grid-gap);
  width: min(100%, calc(100svh - 310px), var(--grid-max));
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--cell);
  overflow: hidden;
}

.cell.wall {
  background:
    linear-gradient(135deg, rgba(95, 64, 32, 0.32), transparent),
    #26341d;
}

.cell.floor {
  background:
    linear-gradient(135deg, rgba(229, 189, 85, 0.05), transparent),
    var(--cell);
}

.cell.player {
  border-color: rgba(143, 208, 107, 0.95);
  box-shadow: inset 0 0 0 2px rgba(143, 208, 107, 0.35);
}

.cell.monster {
  border-color: rgba(217, 119, 85, 0.9);
}

.cell.pickup {
  border-color: rgba(229, 189, 85, 0.9);
  box-shadow: inset 0 0 0 2px rgba(229, 189, 85, 0.18);
}

.cell.chest {
  border-color: rgba(127, 183, 165, 0.95);
  background:
    linear-gradient(135deg, rgba(127, 183, 165, 0.2), transparent),
    #111a15;
  box-shadow: inset 0 0 0 2px rgba(127, 183, 165, 0.14);
}

.cell.selected {
  box-shadow: inset 0 0 0 3px rgba(229, 189, 85, 0.6);
}

.glyph {
  font-weight: 900;
  font-size: clamp(0.86rem, 3vw, 1.35rem);
}

.cell-formula {
  position: absolute;
  right: 5px;
  bottom: 4px;
  max-width: calc(100% - 10px);
  color: var(--muted);
  font-size: 0.68rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.monster-panel {
  min-height: 146px;
  overflow: hidden;
}

.rule-panel,
.log-panel {
  min-height: 0;
}

.rule-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  flex: 1;
  overflow: hidden;
}

.monster-readout {
  display: grid;
  gap: 8px;
  max-height: 100%;
  overflow: auto;
  scrollbar-width: thin;
}

.formula {
  color: var(--gold);
  font-size: 1.35rem;
  overflow-wrap: anywhere;
}

.editor-panel {
  overflow: hidden;
}

.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
}

.button-row {
  display: flex;
  gap: 8px;
}

#castButton {
  background: #315128;
  color: #f0ffe2;
  font-weight: 700;
}

textarea {
  display: block;
  width: 100%;
  min-height: 0;
  height: clamp(120px, 24svh, 220px);
  resize: vertical;
  border: 0;
  outline: 0;
  padding: 16px;
  background: #0d1309;
  color: var(--ink);
  line-height: 1.45;
}

.output-grid {
  display: grid;
  grid-template-rows: minmax(180px, 1fr) minmax(180px, 1fr);
  gap: 14px;
  min-height: 0;
  flex: 1;
}

.log-panel {
  min-height: 0;
  overflow: hidden;
}

pre,
ol {
  height: calc(100% - 28px);
  margin: 0;
  overflow: auto;
}

pre {
  color: #e7ffd4;
  font-size: 0.88rem;
  line-height: 1.45;
  white-space: pre-wrap;
}

ol {
  padding-left: 24px;
}

li {
  margin: 0 0 8px;
  color: var(--muted);
}

li strong {
  color: var(--ink);
}

@media (max-width: 1180px) {
  .dungeon-app {
    grid-template-columns: minmax(0, 1.22fr) minmax(240px, 0.78fr);
    grid-template-rows: auto minmax(0, 1fr) minmax(118px, auto) minmax(0, 64px);
    gap: 8px;
    padding: 8px;
  }

  .left-panel,
  .center-panel,
  .right-panel {
    display: contents;
  }

  .masthead {
    grid-column: 1;
    grid-row: 1;
    padding: 10px 14px;
  }

  .masthead .eyebrow,
  .masthead .muted,
  .section-label {
    display: none;
  }

  h1 {
    font-size: 1.45rem;
    line-height: 1;
  }

  .stats-panel {
    grid-column: 2;
    grid-row: 1;
  }

  .panel {
    padding: 8px;
  }

  .stats {
    gap: 6px;
  }

  .stats div {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 5px;
    padding: 7px 8px;
  }

  .stats span {
    margin: 0;
    font-size: 0.7rem;
  }

  .stats strong {
    font-size: 1rem;
  }

  .dungeon-stage {
    grid-column: 1;
    grid-row: 2;
    height: 100%;
    padding: 8px;
  }

  .monster-panel {
    grid-column: 2;
    grid-row: 2;
    min-height: 0;
  }

  .goals-panel {
    grid-column: 1;
    grid-row: 3;
    min-height: 0;
  }

  .editor-panel {
    grid-column: 2;
    grid-row: 3;
  }

  .editor-head {
    padding: 6px;
    font-size: 0.82rem;
  }

  .button-row {
    gap: 6px;
  }

  button {
    min-height: 34px;
    padding: 0 8px;
    font-size: 0.8rem;
  }

  .output-grid {
    grid-column: 2;
    grid-row: 4;
    grid-template-rows: minmax(0, 1fr);
    gap: 0;
  }

  .output-grid .log-panel:last-child {
    display: none;
  }

  .rule-panel {
    grid-column: 1;
    grid-row: 4;
    height: 100%;
  }

  .rule-list {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-auto-rows: minmax(34px, max-content);
    height: 100%;
    gap: 5px;
    overflow: auto;
  }

  .rule-card {
    min-height: 0;
    padding: 6px;
    text-align: center;
  }

  .rule-card .muted {
    display: none;
  }

  .rule-card strong {
    margin: 0;
    font-size: 0.72rem;
    line-height: 1.05;
  }

  .dungeon-grid {
    --grid-max: 470px;
    width: min(100%, calc(100svh - 220px), var(--grid-max));
  }

  textarea {
    height: clamp(64px, 13svh, 100px);
    max-height: 100px;
    resize: none;
  }

  pre,
  ol {
    height: 100%;
  }
}

@media (min-width: 1181px) and (max-height: 850px) {
  .dungeon-app {
    gap: 10px;
    padding: 10px;
  }

  .left-panel,
  .center-panel,
  .right-panel {
    gap: 8px;
  }

  .center-panel {
    grid-template-rows: minmax(0, auto) minmax(160px, 0.32fr);
  }

  .masthead,
  .panel {
    padding: 10px;
  }

  .masthead .muted,
  .section-label,
  .rule-card .muted {
    display: none;
  }

  h1 {
    font-size: 1.7rem;
  }

  .stats div,
  .rule-card {
    padding: 7px;
  }

  .rule-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: minmax(38px, max-content);
    height: 100%;
    overflow: auto;
  }

  .rule-card strong {
    margin: 0;
    font-size: 0.78rem;
  }

  .dungeon-grid {
    width: min(100%, calc(100svh - 280px), var(--grid-max));
  }

  textarea {
    height: clamp(92px, 18svh, 140px);
    max-height: 140px;
    resize: none;
  }
}

@media (max-width: 780px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .dungeon-app {
    display: grid;
    grid-template-columns: minmax(0, 1.32fr) minmax(200px, 0.68fr);
    grid-template-rows: auto minmax(0, 1fr) minmax(96px, auto) minmax(0, 56px);
    gap: 6px;
    height: 100svh;
    min-height: 0;
    overflow: hidden;
    padding: 6px;
  }

  .left-panel,
  .center-panel,
  .right-panel {
    display: contents;
  }

  .masthead {
    grid-column: 1;
    grid-row: 1;
    order: 1;
    padding: 7px 10px;
  }

  .stats-panel {
    grid-column: 2;
    grid-row: 1;
    order: 2;
  }

  .dungeon-stage {
    grid-column: 1;
    grid-row: 2;
    order: 3;
  }

  .monster-panel {
    grid-column: 2;
    grid-row: 2;
    order: 4;
  }

  .goals-panel {
    grid-column: 1;
    grid-row: 3;
    order: 5;
    min-height: 0;
  }

  .editor-panel {
    grid-column: 2;
    grid-row: 3;
    order: 6;
  }

  .output-grid {
    grid-column: 2;
    grid-row: 4;
    order: 8;
  }

  .rule-panel {
    grid-column: 1;
    grid-row: 4;
    order: 7;
    height: 100%;
  }

  .right-panel {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 1.28rem;
    line-height: 1;
  }

  .eyebrow,
  .section-label,
  .masthead .muted {
    display: none;
  }

  .panel {
    padding: 6px 8px;
    overflow: hidden;
  }

  .stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  .stats div {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 4px;
    padding: 6px;
  }

  .stats span {
    margin: 0;
    font-size: 0.68rem;
  }

  .stats strong {
    font-size: 1rem;
  }

  .dungeon-stage {
    height: 100%;
    min-height: auto;
    padding: 4px;
  }

  .dungeon-grid {
    --grid-gap: clamp(2px, 1vw, 4px);
    --grid-max: min(100%, calc(100svh - 170px), 430px);
    width: min(100%, calc(100svh - 205px), var(--grid-max));
  }

  .cell {
    border-radius: 6px;
  }

  .glyph {
    font-size: 0.9rem;
  }

  .cell-formula {
    right: 3px;
    bottom: 2px;
    font-size: clamp(0.46rem, 1.8vw, 0.56rem);
  }

  .monster-panel {
    min-height: auto;
  }

  .monster-readout {
    gap: 2px;
  }

  .monster-readout p {
    margin: 0;
    font-size: 0.76rem;
    line-height: 1.25;
  }

  .monster-readout p:first-of-type {
    display: none;
  }

  .formula {
    font-size: 0.92rem;
  }

  .rule-list {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-auto-rows: minmax(30px, max-content);
    max-height: none;
    height: 100%;
    overflow: auto;
    gap: 4px;
  }

  .rule-card {
    min-height: 0;
    padding: 5px;
    text-align: center;
  }

  .rule-card strong {
    margin: 0;
    font-size: 0.66rem;
    line-height: 1.05;
  }

  .rule-pattern {
    display: block;
  }

  .rule-card .muted {
    display: none;
  }

  .editor-head {
    align-items: stretch;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 5px;
    padding: 5px;
    font-size: 0.78rem;
  }

  .editor-head > span {
    display: none;
  }

  .button-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  button {
    min-height: 34px;
    padding: 0 6px;
    font-size: 0.76rem;
  }

  textarea {
    height: clamp(52px, 14svh, 78px);
    min-height: 52px;
    max-height: 78px;
    resize: none;
    padding: 8px;
    font-size: 0.82rem;
  }

  .output-grid {
    display: grid;
    grid-template-rows: minmax(0, 1fr);
    gap: 0;
    min-height: 0;
  }

  .output-grid .log-panel:last-child {
    display: none;
  }

  .log-panel {
    display: grid;
    padding: 6px 8px;
  }

  pre,
  ol {
    height: 100%;
    font-size: 0.72rem;
    line-height: 1.25;
  }
}

@media (max-width: 540px) {
  html,
  body {
    height: auto;
    min-height: 100%;
    overflow: auto;
  }

  .masthead {
    display: none;
  }

  .dungeon-app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows:
      auto
      auto
      auto
      auto
      auto
      auto;
    height: auto;
    min-height: 100svh;
    overflow: visible;
  }

  .stats-panel,
  .dungeon-stage,
  .monster-panel,
  .goals-panel,
  .rule-panel,
  .editor-panel,
  .output-grid {
    grid-column: 1;
  }

  .stats-panel {
    grid-row: 1;
  }

  .dungeon-stage {
    grid-row: 2;
    height: auto;
    overflow: visible;
  }

  .monster-panel {
    grid-row: 3;
  }

  .goals-panel {
    display: none;
  }

  .rule-panel {
    grid-row: 4;
    height: auto;
  }

  .editor-panel {
    grid-row: 5;
  }

  .output-grid {
    grid-row: 6;
  }

  .dungeon-grid {
    --grid-gap: clamp(3px, 1.2vw, 5px);
    --grid-max: 430px;
    width: min(100%, 92vw, var(--grid-max));
  }

  .glyph {
    font-size: 0.82rem;
  }

  button {
    min-height: 30px;
  }

  textarea {
    height: 42px;
    min-height: 42px;
    max-height: 42px;
    padding: 5px 8px;
  }
}
