:root {
  color-scheme: dark;
  --bg: #0c1013;
  --panel: #171c21;
  --panel-2: #20272e;
  --cell: #10161a;
  --ink: #eef5ef;
  --muted: #9ca9a6;
  --line: #303a43;
  --green: #63d297;
  --gold: #f5c76b;
  --red: #ef765f;
  --blue: #77a6f7;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background:
    linear-gradient(135deg, rgba(99, 210, 151, 0.09), transparent 34%),
    linear-gradient(315deg, rgba(119, 166, 247, 0.1), 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;
}

.masthead,
.panel,
.editor-panel,
.dungeon-stage {
  background: rgba(23, 28, 33, 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: #0b0f12;
  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;
}

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

.rule-card {
  padding: 10px;
  background: #0b0f12;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.rule-card strong {
  display: block;
  margin-bottom: 5px;
}

.rule-card:disabled {
  color: #61706b;
}

.rule-card:disabled .rule-pattern,
.rule-card:disabled .muted {
  color: #61706b;
}

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: #61706b;
  cursor: not-allowed;
}

.dungeon-stage {
  display: grid;
  place-items: center;
  min-height: 0;
  flex: 1;
  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 - 230px), var(--grid-max));
  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: #252b31;
}

.cell.floor {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.03), transparent),
    var(--cell);
}

.cell.player {
  border-color: rgba(99, 210, 151, 0.95);
  box-shadow: inset 0 0 0 2px rgba(99, 210, 151, 0.35);
}

.cell.monster {
  border-color: rgba(239, 118, 95, 0.9);
}

.cell.pickup {
  border-color: rgba(245, 199, 107, 0.9);
  box-shadow: inset 0 0 0 2px rgba(245, 199, 107, 0.18);
}

.cell.selected {
  box-shadow: inset 0 0 0 3px rgba(245, 199, 107, 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;
}

.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;
}

.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: #254331;
  color: #e5ffed;
  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: #090d10;
  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: #ddffe8;
  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) 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 / 4;
    height: 100%;
    padding: 8px;
  }

  .monster-panel {
    grid-column: 2;
    grid-row: 2;
    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 - 160px), 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;
  }

  .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 - 150px), 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) 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 / 4;
    order: 3;
  }

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

  .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: 5;
    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 - 138px), 430px);
  }

  .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: 420px) {
  .masthead {
    display: none;
  }

  .dungeon-app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows:
      auto
      minmax(0, 1fr)
      auto
      minmax(0, 44px)
      auto
      minmax(0, 46px);
  }

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

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

  .dungeon-stage {
    grid-row: 2;
  }

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

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

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

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

  .dungeon-grid {
    --grid-max: min(92vw, calc(100svh - 260px), 320px);
  }

  .glyph {
    font-size: 0.82rem;
  }

  button {
    min-height: 30px;
  }

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