:root {
  --ink: #24180f;
  --paper: #f3ead7;
  --paper-2: #e7dcc4;
  --brass: #a97832;
  --brass-2: #d7b06a;
  --felt: #1d4634;
  --wood: #3a2618;
  --danger: #8d2f24;
  --ok: #d7b06a;
  color-scheme: dark;
  font-family: "Segoe UI", "Avenir Next", "Trebuchet MS", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--ink); color: var(--paper); }
button, input { font: inherit; font-size: 16px; }
button { cursor: pointer; }
h1, h2 { font-family: Palatino, "Iowan Old Style", Georgia, serif; font-weight: 600; }
h1 { margin: 0 0 0.3rem; font-size: 1.8rem; line-height: 1.15; }
h2 { margin: 0 0 0.6rem; font-size: 1.15rem; }

.screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at top, #4a301c 0%, transparent 42%),
    var(--ink);
}
.card {
  width: min(880px, 100%);
  background: #2c1d13;
  border: 1px solid #6b4a2c;
  border-radius: 18px;
  padding: 28px 24px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}
.card.narrow { width: min(440px, 100%); }
.eyebrow { margin: 0 0 0.4rem; letter-spacing: 0.14em; text-transform: uppercase; font-size: 0.75rem; color: var(--brass-2); }
.lede { margin: 0 0 1.2rem; color: #eadcc4; }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
label { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; color: #eadcc4; font-size: 0.92rem; }
input[type="text"], input[type="password"], input[type="number"], input:not([type]) {
  background: var(--paper);
  color: var(--ink);
  border: 0;
  border-radius: 8px;
  padding: 10px 12px;
}
button {
  background: var(--brass);
  color: #1a100a;
  border: 0;
  border-radius: 999px;
  padding: 10px 16px;
  font-weight: 700;
}
button.secondary { background: transparent; color: var(--paper); border: 1px solid #8a6844; }
.hint { color: #cbb89a; font-size: 0.88rem; }
.rejoin { margin: 0 0 12px; }
.rejoin .eyebrow { margin-bottom: 8px; }
.rejoin-list { display: flex; flex-wrap: wrap; gap: 8px; }
.rejoin-list button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #3a2618;
  color: var(--paper);
  border: 1px solid #d7b06a;
}
.rejoin-list i {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: inline-block;
}
.text-link {
  display: inline-block;
  background: var(--brass);
  color: #1a100a;
  border-radius: 999px;
  padding: 10px 16px;
  font-weight: 700;
  text-decoration: none;
}
.error { min-height: 1.2em; color: #ffb4a8; margin: 8px 0 0; }
.check { flex-direction: row; align-items: center; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #14110e;
}
.top {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid #3a2a1c;
  background: #1c1612;
}
.top h1 { font-size: 1.2rem; }
.brand { min-width: 8rem; }
.adventure { margin: 0.15rem 0 0; color: var(--brass-2); font-size: 0.92rem; }
.top-actions, .people { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.top-actions label { margin: 0; flex-direction: row; align-items: center; }
.top-actions input:not([type]), #my-name { width: 8rem; padding: 6px 8px; }
.person, .conn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #2a211a;
  border-radius: 999px;
  padding: 3px 8px 3px 3px;
  font-size: 0.85rem;
}
.person i {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: white;
  font-style: normal;
  font-size: 10px;
  font-weight: 700;
}
.conn { padding: 4px 10px; }
.conn.on { color: #c8f5c4; }
.conn.off { color: #ffb4a8; }

.workspace {
  position: relative;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
}
.panel { min-height: 0; }
.map-panel {
  position: relative;
  display: flex;
  flex-direction: column;
  background: #0e1210;
  min-height: 0;
}
.map-hud {
  position: absolute;
  z-index: 3;
  top: 8px;
  left: 8px;
  right: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}
.map-hud > * { pointer-events: auto; }
.zoom-controls, #gm-tools, #npc-form {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  background: rgba(20, 16, 12, 0.88);
  border: 1px solid #5a3b24;
  border-radius: 12px;
  padding: 6px;
}
#gm-tools[hidden] { display: none; }
.map-hud label { margin: 0; flex-direction: row; align-items: center; color: var(--paper); }
.map-hud input[type="number"] { width: 4.2rem; padding: 6px; }
.map-hud input:not([type]) { width: 7rem; padding: 6px; }
#npc-form input[type="color"], #my-color, #npc-form input[name="color"] {
  width: 36px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: 0;
}
#viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  user-select: none;
}
#viewport.panning { cursor: grabbing; }
#dice-stage {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  visibility: hidden;
}
#dice-stage.on { visibility: visible; }
#dice-stage canvas { display: block; width: 100%; height: 100%; }
.board {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(var(--cols) * var(--cell));
  height: calc(var(--rows) * var(--cell));
  --cols: 16;
  --rows: 12;
  --cell: 52px;
  transform-origin: 0 0;
  background: #1d4634;
  box-shadow: 0 0 0 8px #5a3b24, 0 0 0 10px #d7b06a;
}
.board img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; }
.grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(to right, rgba(243, 234, 215, 0.45) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(243, 234, 215, 0.45) 1px, transparent 1px);
  background-size: calc(100% / var(--cols)) calc(100% / var(--rows));
  pointer-events: none;
}
#tokens { position: absolute; inset: 0; }
.token, .ping {
  position: absolute;
  width: calc(100% / var(--cols));
  height: calc(100% / var(--rows));
  left: calc(var(--col) * 100% / var(--cols));
  top: calc(var(--row) * 100% / var(--rows));
}
.token {
  display: grid;
  place-items: center;
  touch-action: none;
  user-select: none;
  z-index: 2;
  cursor: grab;
}
.token i {
  width: 78%;
  height: 78%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: white;
  font-style: normal;
  font-weight: 700;
  border: 2px solid rgba(255, 255, 255, 0.85);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
}
.token b {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translate(-50%, 2px);
  background: rgba(12, 10, 8, 0.9);
  color: white;
  border-radius: 4px;
  padding: 0 5px;
  font-size: 11px;
  font-weight: 650;
  white-space: nowrap;
  max-width: 160%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.token.npc i { border-radius: 4px; }
.token.mine i { box-shadow: 0 0 0 3px #e7c56a, 0 2px 6px rgba(0, 0, 0, 0.45); }
.token button.remove {
  position: absolute;
  top: 0;
  right: 0;
  width: 20px;
  height: 20px;
  padding: 0;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  line-height: 1;
}
.ping {
  pointer-events: none;
  z-index: 1;
}
.ping::after {
  content: "";
  position: absolute;
  inset: 10%;
  border-radius: 50%;
  border: 3px solid #f2d48a;
  animation: ping 1.4s ease-out forwards;
}
@keyframes ping {
  from { transform: scale(0.4); opacity: 1; }
  to { transform: scale(1.6); opacity: 0; }
}
.map-hint {
  margin: 0;
  padding: 4px 10px 6px;
  color: #b7a48a;
  font-size: 0.78rem;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  background: #f4efe6;
  color: var(--ink);
  border-left: 1px solid #c8b89a;
  min-height: 0;
  overflow: hidden;
}
.chat-panel h2 { padding: 10px 12px 0; }
#log { list-style: none; margin: 0; padding: 8px 12px; overflow: auto; flex: 1; min-height: 0; }
#log li { padding: 8px 0; border-bottom: 1px solid #e4d5b8; }
#log .who { font-weight: 700; }
#log .when { float: right; color: #6d5b45; font-size: 0.78rem; }
#log .say { margin: 2px 0 0; white-space: pre-wrap; }
.dice-card {
  margin-top: 4px;
  background: white;
  border: 1px solid #d9cbb0;
  border-radius: 8px;
  padding: 6px 8px;
}
.dice-card .formula { color: #6d5b45; font-size: 0.85rem; }
.faces { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }
.faces span, .faces s {
  min-width: 1.6rem;
  text-align: center;
  background: #f7f1e4;
  border: 1px solid #c4b496;
  border-radius: 4px;
  padding: 2px 4px;
  text-decoration: none;
  font-variant-numeric: tabular-nums;
}
.faces s { opacity: 0.45; }
.dice-card .total { display: block; text-align: right; font-size: 1.35rem; }
.empty { color: #6d5b45; }
#roll-form { flex-shrink: 0; padding: 8px; background: #efe6d6; border-top: 1px solid #d9cbb0; }
#roll-form label { margin: 0; color: var(--ink); }
.dice-buttons, .dice-options, .chat-line { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.dice-options, .chat-line { margin-top: 6px; }
.dice-buttons button {
  width: 42px;
  height: 36px;
  border-radius: 6px;
  background: white;
  color: var(--ink);
  border: 1px solid #8a6844;
  padding: 0;
}
#count, #mod { width: 4.5rem; }
#dice3d {
  height: 36px;
  border-radius: 6px;
  background: white;
  color: var(--ink);
  border: 1px solid #8a6844;
  padding: 0 10px;
}
#dice3d[aria-pressed="true"] {
  background: var(--brass);
  color: #1a100a;
  border-color: var(--brass);
}
.chat-line input { flex: 1; min-width: 0; }
.chat-panel .error { padding: 0 8px 8px; }
.chat-panel .error.ok { color: #3d6b32; }
.tabs { display: none; }

.library {
  position: absolute;
  z-index: 6;
  inset: 8px;
  display: grid;
}
.library-card {
  min-height: 0;
  background: #2c1d13;
  border: 1px solid #6b4a2c;
  border-radius: 16px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  overflow: hidden;
}
.library-head, .library-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
  justify-content: space-between;
  padding: 14px 16px 0;
}
.library-head h2 { margin: 0; }
.library-filters { padding-top: 10px; }
.library-filters label { margin: 0; min-width: 9rem; flex: 1; }
.library-filters select, .library-filters input {
  background: var(--paper);
  color: var(--ink);
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
}
.library-count { margin: 8px 16px; color: #cbb89a; font-size: 0.88rem; }
.library-body {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(240px, 0.8fr);
  border-top: 1px solid #4a3424;
}
#library-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: auto;
}
.library-row {
  width: 100%;
  display: grid;
  grid-template-columns: 4.6rem minmax(0, 1fr);
  gap: 8px;
  align-items: baseline;
  text-align: left;
  background: transparent;
  color: var(--paper);
  border: 0;
  border-bottom: 1px solid #3d2b1e;
  border-radius: 0;
  padding: 9px 14px;
}
.library-row.current { background: #4a301c; }
.library-row .code { color: var(--brass-2); font-weight: 700; }
.library-row .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-detail {
  overflow: auto;
  padding: 16px;
  border-left: 1px solid #4a3424;
  background: #24180f;
}
.library-detail h3 { margin: 0 0 0.4rem; font-size: 1.25rem; }
#library-meta { color: #eadcc4; }
#open-pdf {
  display: inline-block;
  background: var(--brass);
  color: #1a100a;
  border-radius: 999px;
  padding: 10px 16px;
  font-weight: 700;
  text-decoration: none;
}
#clear-adventure { margin-top: 8px; }
.library-detail .file { margin-top: 12px; }

@media (max-width: 860px) {
  .split { grid-template-columns: 1fr; }
  .top { align-items: stretch; flex-direction: column; }
  .workspace { grid-template-columns: 1fr; }
  .panel { display: none; }
  .panel.active { display: flex; }
  .map-hint { display: none; }
  .tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    background: #14110e;
  }
  .tabs button { width: 100%; }
  .tabs button.active { background: var(--brass-2); }
  .tabs { grid-template-columns: 1fr 1fr 1.15fr; }
  #leave-table { display: none; }
  .app { grid-template-rows: auto minmax(0, 1fr) auto; }
  .library { inset: 4px; }
  .library-head, .library-filters { padding-left: 10px; padding-right: 10px; }
  .library-filters {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .library-filters label:first-child { grid-column: 1 / -1; }
  .library-body { grid-template-columns: 1fr; grid-template-rows: minmax(120px, 1fr) auto; }
  .library-detail { border-left: 0; border-top: 1px solid #4a3424; max-height: 46%; }
}
