:root {
  --bg: #0e0c0a;
  --panel: #1c1814;
  --text: #f4eadc;
  --muted: #a89884;
  --accent: #d7b07a;
  --line: #3a322a;
  --input: #241e18;
  --ui: "Segoe UI", system-ui, sans-serif;
  --story: Georgia, "Times New Roman", serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #0e0c0a;
  background-image: radial-gradient(ellipse at 50% 18%, #1a1612 0%, #0e0c0a 68%);
  color: var(--text);
  font-family: var(--story);
}

.app {
  position: relative;
  z-index: 1;
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 16px 40px;
}

#backdrop {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: #0e0c0a;
}

.backdrop-layer {
  position: absolute;
  inset: 0;
  background-position: center;
  background-size: cover;
  opacity: 0;
  transition: opacity 600ms ease;
}

.backdrop-layer.show {
  opacity: 1;
}

.backdrop-shade {
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, rgba(8, 6, 5, 0.22), rgba(8, 6, 5, 0.58));
}

#backdrop.tense .backdrop-shade {
  background: linear-gradient(to bottom, rgba(48, 18, 14, 0.5), rgba(8, 6, 5, 0.8));
}

.sound-toggle {
  position: fixed;
  top: 12px;
  right: 16px;
  z-index: 30;
}

.sound-panel {
  position: fixed;
  top: 46px;
  right: 16px;
  z-index: 30;
  width: 240px;
  padding: 12px 12px 6px;
  background: rgba(22, 19, 15, 0.96);
  border: 1px solid #4a3f34;
}

.sound-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 13px;
}

.sound-row input[type="range"] {
  flex: 1;
  width: auto;
  margin: 0;
  padding: 0;
}

.sound-row input[type="checkbox"] {
  width: auto;
  flex: none;
  margin: 0;
}

.sound-panel .ghost-button {
  min-width: 28px;
  padding: 4px 8px;
}

body.in-play {
  overflow: hidden;
}

body.in-play .app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 14px 16px 0;
  overflow: hidden;
}

body.in-play .subtitle {
  display: none;
}

body.in-play header h1 {
  margin-bottom: 8px;
  font-size: 16px;
}

body.in-play #game-screen {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

header h1 {
  margin: 0;
  font-family: var(--ui);
  font-size: 22px;
  font-weight: 500;
  letter-spacing: 0.04em;
}

.subtitle {
  margin: 8px 0 28px;
  color: var(--muted);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.round-label {
  margin: 0;
  font-family: var(--ui);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.adventure {
  background: transparent;
  border: 0;
  padding: 0 0 8px;
  margin: 0;
}

.adventure h2 {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 28px;
  font-weight: normal;
  letter-spacing: 0.01em;
}

.adventure .location {
  margin: 0 0 8px;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 13px;
}

.adventure .intro {
  margin: 0;
  line-height: 1.65;
  white-space: pre-wrap;
}

#lobby {
  margin-bottom: 8px;
}

.ghost-button {
  margin: 0;
  padding: 6px 0;
  background: transparent;
  color: var(--muted);
  border: 0;
  border-radius: 0;
  font-family: var(--ui);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.ghost-button:hover {
  color: var(--text);
  filter: none;
}

.hint {
  margin: 6px 0 16px;
  color: var(--muted);
}

.stage {
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr) 176px;
  gap: 18px;
  align-items: start;
}

body.in-play .stage {
  flex: 1;
  min-height: 0;
  align-items: stretch;
}

.scene {
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.scene-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}

.party-col,
.side-rail {
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: sticky;
  top: 12px;
  max-height: 100%;
  overflow: auto;
}

.rail-button {
  width: 100%;
  background: #2a241c;
  color: var(--text);
  border: 1px solid #4a3f34;
  font-family: var(--ui);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 12px;
}

.die-window,
.turn-window {
  border: 1px solid #4a3f34;
  background: rgba(28, 24, 20, 0.94);
  padding: 12px 8px 10px;
  text-align: center;
}

.rail-caption {
  margin: 0;
  font-family: var(--ui);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.die-face,
.turn-count {
  margin: 8px 0 4px;
  font-family: var(--ui);
  font-size: 42px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.die-note {
  margin: 0;
  min-height: 1.2em;
  font-family: var(--ui);
  font-size: 12px;
  color: var(--muted);
}

.die-window.tone-success .die-face,
.die-window.tone-success .die-note {
  color: #9cba8e;
}
.die-window.tone-failure .die-face,
.die-window.tone-failure .die-note {
  color: #e0b0a2;
}
.die-window.tone-crit-success .die-face,
.die-window.tone-crit-success .die-note {
  color: #e6c98a;
}
.die-window.tone-crit-failure .die-face,
.die-window.tone-crit-failure .die-note {
  color: #e7b2a4;
}
.die-window.tone-discovery .die-face,
.die-window.tone-discovery .die-note {
  color: #9eb6d4;
}
.die-window.tone-combat .die-face,
.die-window.tone-combat .die-note {
  color: #e0b08a;
}

.create-layout {
  display: grid;
  grid-template-columns: minmax(0, 680px) minmax(180px, 1fr);
  gap: 28px;
  align-items: start;
}

.class-portrait {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 12px;
}

.class-portrait img {
  width: min(420px, 100%);
  height: auto;
  background: #14110e;
  border: 1px solid #4a3f34;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.class-portrait p {
  margin: 10px 0 0;
  font-family: var(--ui);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
}

#log {
  flex: 1;
  min-height: 280px;
  height: min(52vh, 520px);
  overflow-y: auto;
  background: rgba(28, 24, 20, 0.94);
  border: 1px solid #4a3f34;
  box-shadow: inset 0 0 0 1px rgba(215, 176, 122, 0.06), 0 18px 40px rgba(0, 0, 0, 0.28);
  border-radius: 2px;
  padding: 22px 26px 28px;
  font-size: 17px;
}

body.in-play #log {
  height: auto;
  min-height: 0;
}

#chronicle {
  max-height: none;
  overflow: visible;
}

#chronicle p {
  margin: 0 0 6px;
  line-height: 1.45;
}

#chronicle article {
  margin: 0 0 16px;
  padding: 0;
  border: 0;
}

#chronicle .round-title {
  margin: 16px 0 6px;
}

#chronicle .game-master {
  padding: 0;
  border: 0;
}

h2 {
  margin: 0 0 12px;
  font-size: 18px;
  font-weight: normal;
  color: var(--text);
}

h3 {
  margin: 18px 0 8px;
  font-family: var(--ui);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

#log p {
  margin: 0 0 10px;
  line-height: 1.65;
}

#log .system {
  color: var(--muted);
  font-style: italic;
}

#log .check,
#chronicle .check {
  color: var(--muted);
}

#log .harm,
#chronicle .harm,
.death-note {
  color: #e0b0a2;
}

.hero-card.dead {
  opacity: 0.62;
}

.status-dead,
.status.dead {
  color: #e0b0a2;
}

#log .game-master {
  margin-top: 8px;
  padding: 0;
  border: 0;
  color: var(--text);
  font-size: 1em;
  line-height: 1.7;
  max-width: 68ch;
}

#log .quote,
#log .dice-card,
#log .round-title,
#log .round-done {
  max-width: 68ch;
}

.arrive {
  animation: arrive 0.4s ease;
}

body.no-motion .arrive {
  animation: none;
}

@keyframes arrive {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

body[data-text="small"] #log {
  font-size: 15px;
}

body[data-text="large"] #log {
  font-size: 20px;
}

.beat {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 8px 0 4px;
  min-width: 0;
}

.dice-card {
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
  border: 1px solid var(--line);
  border-left-width: 2px;
  border-radius: 2px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
}

#log .dice-card p {
  margin: 0 0 4px;
  color: var(--text);
  line-height: 1.4;
}

.dice-kicker {
  font-family: var(--ui);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.dice-note {
  color: var(--text);
}

.dice-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin: 6px 0 2px;
}

.dice-grid p {
  margin: 0;
  text-align: center;
}

.dice-grid span {
  display: block;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 11px;
}

.dice-grid strong {
  display: block;
  font-family: var(--ui);
  font-size: 16px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.dice-result {
  margin-bottom: 0;
  font-family: var(--ui);
  font-size: 13px;
  font-weight: 500;
}

.tone-success {
  border-left-color: #7d9a72;
}
#log .tone-success .dice-kicker,
#log .tone-success .dice-result {
  color: #9cba8e;
}

.tone-failure {
  border-left-color: #a85a52;
}
#log .tone-failure .dice-kicker,
#log .tone-failure .dice-result {
  color: #e0b0a2;
}

.tone-crit-success {
  border-left-color: #d7b07a;
  box-shadow: inset 0 0 0 1px rgba(215, 176, 122, 0.35);
}
#log .tone-crit-success .dice-kicker,
#log .tone-crit-success .dice-result {
  color: #e6c98a;
}

.tone-crit-failure {
  border-left-color: #8a3030;
  box-shadow: inset 0 0 0 1px rgba(138, 48, 48, 0.45);
}
#log .tone-crit-failure .dice-kicker,
#log .tone-crit-failure .dice-result {
  color: #e7b2a4;
}

.tone-discovery {
  border-left-color: #6a8fbf;
}
#log .tone-discovery .dice-kicker,
#log .tone-discovery .dice-result {
  color: #9eb6d4;
}

.tone-combat {
  border-left-color: #c4845a;
}
#log .tone-combat .dice-kicker,
#log .tone-combat .dice-result {
  color: #e0b08a;
}

.tone-loot {
  border-left-color: #8d74a8;
}
#log .tone-loot .dice-kicker {
  color: #c4b0d4;
}

.tone-important {
  border-left-color: #d7b07a;
  border-top: 1px solid rgba(215, 176, 122, 0.4);
  border-bottom: 1px solid rgba(215, 176, 122, 0.4);
}
#log .tone-important .dice-kicker {
  color: #e6c98a;
}

.tone-death {
  border-color: #6e2a2a;
  border-left-width: 2px;
  background: rgba(80, 28, 28, 0.28);
}
#log .tone-death .dice-kicker,
#log .tone-death .dice-result {
  color: #e7b2a4;
}

.round-title,
.round-done {
  margin: 18px 0 6px;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.quote {
  margin: 0 0 6px;
  color: var(--muted);
  font-style: italic;
  line-height: 1.45;
}

.quote-name {
  font-style: normal;
  font-family: var(--ui);
  font-size: 12px;
  letter-spacing: 0.04em;
}

#players {
  margin: 0;
  padding: 0;
  list-style: none;
}

#players li {
  margin-bottom: 10px;
  line-height: 1.4;
}

#players .me {
  color: var(--accent);
}

#players .waiting strong {
  color: var(--muted);
}

label {
  display: block;
  margin-bottom: 8px;
  font-family: var(--ui);
  font-size: 13px;
}

.row {
  display: flex;
  gap: 8px;
}

input,
select {
  width: 100%;
  flex: 1;
  min-width: 0;
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: var(--input);
  color: var(--text);
  font: inherit;
}

#create-form {
  max-width: 760px;
}

.field-label {
  margin: 4px 0 8px;
  font-family: var(--ui);
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.class-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.class-card,
.hero-card {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 2px;
  color: var(--text);
  font-family: var(--ui);
  font-weight: normal;
  text-align: left;
  padding: 12px;
  box-shadow: none;
}

.hero-card {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 10px;
}

.hero-card.me {
  background: #2a241c;
  border-color: #5c4c38;
}

.avatar,
.hero-card .avatar,
.class-card .avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex: none;
  display: block;
}

.modal-card h2 {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hero-card .who {
  min-width: 0;
  flex: 1;
}

.hero-card .who strong,
.hero-card .who span {
  margin-bottom: 2px;
  font-size: 13px;
}

.hero-card .who strong {
  font-size: 15px;
  font-weight: 500;
}

.class-card.selected,
.hero-card.selected {
  border-color: var(--accent);
}

.class-card strong,
.hero-card strong,
.class-card span,
.hero-card span {
  display: block;
  margin-bottom: 6px;
}

.class-card strong,
.hero-card strong {
  font-size: 18px;
  font-weight: 500;
}

.class-hp {
  color: var(--text);
  font-size: 13px;
}

.hp-line {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 4px 0;
}

.hp-bar {
  flex: 1;
  height: 3px;
  background: #3a322a;
  overflow: hidden;
}

.hp-bar i {
  display: block;
  height: 100%;
  background: #9cba8e;
}

.hero-card.dead .hp-bar i {
  background: #6a5348;
}

.hp-num {
  font-family: var(--ui);
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-family: var(--ui);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.status i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #6a5a48;
  flex: none;
}

.status.ready {
  color: #9cba8e;
}

.status.ready i {
  background: #9cba8e;
}

.status.dead i {
  background: #a85a52;
}

.muted {
  color: var(--muted);
}

.stat-table {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.stat-table p {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
  font-family: var(--ui);
}

.stat-table p span:first-child {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.stat-table p span:nth-child(2) {
  font-size: 20px;
}

.inventory {
  margin: 0;
  padding-left: 0;
  list-style: none;
}

.inventory li {
  margin: 0 0 6px;
}

.modal,
.drawer {
  position: fixed;
  inset: 0;
  z-index: 20;
}

.modal {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 6, 5, 0.72);
}

.drawer {
  background: rgba(8, 6, 5, 0.45);
}

.modal-card,
.drawer aside {
  background: #16130f;
  border: 1px solid #4a3f34;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.42);
  animation: rise 0.16s ease-out;
}

.modal-card {
  width: min(440px, 100%);
  max-height: min(82vh, 680px);
  overflow: auto;
  border-radius: 2px;
  padding: 18px 18px 20px;
}

.drawer aside {
  width: min(360px, 100%);
  height: 100%;
  overflow: auto;
  padding: 18px 18px 24px;
  border-width: 0 1px 0 0;
}

.drawer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.drawer-bar h2,
.modal-card h2 {
  margin: 0;
  font-size: 22px;
}

.action-dock {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr) 176px;
  column-gap: 18px;
  row-gap: 4px;
  align-items: center;
  max-width: none;
  margin: 0;
  padding: 12px 0 16px;
  background: linear-gradient(to top, #0e0c0a 72%, rgba(14, 12, 10, 0));
  text-align: left;
}

.dock-line {
  grid-column: 2;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.action-dock label {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.action-dock input {
  flex: 1;
  width: auto;
  margin: 0;
  padding: 10px 2px;
  border: 0;
  border-bottom: 1px solid #6a5438;
  border-radius: 0;
  background: transparent;
  text-align: left;
  font-family: var(--story);
}

.action-dock input:focus {
  outline: none;
  border-bottom-color: var(--accent);
}

.action-dock button {
  min-width: 132px;
  margin: 0;
}

.action-dock .hint {
  grid-column: 2;
  margin: 0;
  font-family: var(--ui);
  font-size: 13px;
}

@keyframes rise {
  from {
    transform: translateY(8px);
    opacity: 0.65;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

#create-form button[type="submit"] {
  width: 100%;
}

.row input,
.row select {
  margin-bottom: 0;
}

button {
  padding: 10px 16px;
  border: 1px solid #6a5438;
  border-radius: 2px;
  background: #3a2e22;
  color: var(--text);
  font-family: var(--ui);
  font-weight: 500;
  cursor: pointer;
}

#ready-button,
#start-adventure,
#create-form button[type="submit"] {
  background: var(--accent);
  color: #1a140e;
  border: 0;
}

button:hover {
  filter: brightness(1.08);
}

button:disabled,
input:disabled {
  opacity: 0.55;
  cursor: default;
}

.class-card,
.hero-card {
  background: var(--panel);
  color: var(--text);
  font-weight: normal;
  border: 1px solid var(--line);
}

.hero-card.me {
  background: #2a241c;
  border-color: #5c4c38;
}

.hero-card .hp-line,
.hero-card .status {
  display: flex;
  font-size: 11px;
}

.hero-card .hp-bar {
  display: block;
  flex: 1;
  margin-bottom: 0;
}

.ghost-button,
.scene-bar .ghost-button,
.drawer-bar .ghost-button {
  margin-top: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  border: 0;
}

#action-form {
  margin-top: 0;
}

@media (prefers-reduced-motion: reduce) {
  .arrive,
  .backdrop-layer {
    animation: none;
    transition: none;
  }
}

@media (max-width: 860px) {
  body.in-play {
    overflow: auto;
  }

  body.in-play .app {
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

  body.in-play .stage {
    flex: none;
    align-items: flex-start;
    align-content: flex-start;
  }

  .stage {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 10px;
  }

  .party-col {
    position: static;
    flex: 1 1 100%;
    flex-direction: row;
    align-items: flex-start;
    overflow-x: auto;
    order: 1;
    max-height: none;
  }

  .side-rail {
    position: static;
    order: 2;
    flex: 1 1 100%;
    flex-direction: row;
    align-items: stretch;
    max-height: none;
  }

  .scene {
    order: 3;
    flex: 1 1 100%;
  }

  .die-face,
  .turn-count {
    font-size: 28px;
  }

  .create-layout {
    grid-template-columns: 1fr;
  }

  .class-portrait {
    order: -1;
    padding-top: 0;
  }

  .class-portrait img {
    width: min(300px, 100%);
  }

  .hero-card {
    min-width: 0;
  }

  .class-grid {
    grid-template-columns: 1fr;
  }

  #log,
  body.in-play #log {
    height: 52vh;
    min-height: 240px;
  }

  .stat-table {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .modal-card,
  .drawer aside {
    width: 100%;
  }

  .action-dock {
    position: sticky;
    bottom: 0;
    grid-template-columns: minmax(0, 1fr);
  }

  .dock-line,
  .action-dock .hint {
    grid-column: 1;
  }
}
