:root {
  --page: #f4f6f3;
  --ink: #151515;
  --muted: #6d716b;
  --panel: #ffffff;
  --line: #d7ddd2;
  --accent: #117c6f;
  --accent-strong: #0a5c52;
  --coral: #d95f4b;
  --gold: #d39b28;
  --stage-edge: #161715;
  --tile-size: clamp(46px, 4.8vw, 72px);
  --shadow: 0 18px 50px rgba(25, 29, 24, 0.16);
  color-scheme: light;
  font-family:
    Inter, "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background:
    linear-gradient(135deg, rgba(17, 124, 111, 0.08), transparent 34%),
    linear-gradient(315deg, rgba(217, 95, 75, 0.1), transparent 38%),
    var(--page);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button,
.ghost-link {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  text-decoration: none;
  transition:
    transform 160ms ease,
    border-color 160ms ease,
    background 160ms ease,
    color 160ms ease;
}

button:hover,
.ghost-link:hover {
  border-color: #aeb9a7;
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
.ghost-link:focus-visible {
  outline: 3px solid rgba(17, 124, 111, 0.22);
  outline-offset: 2px;
}

.primary-action,
.stage-roll {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.primary-action:hover,
.stage-roll:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.danger-button {
  color: #8f2c1e;
}

.topbar {
  align-items: center;
  display: flex;
  gap: 24px;
  justify-content: space-between;
  padding: 24px clamp(18px, 4vw, 48px) 14px;
}

.eyebrow {
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  margin: 0 0 4px;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(25px, 3vw, 40px);
  line-height: 1.08;
}

.topbar-actions,
.button-row,
.stage-toolbar,
.mini-readout {
  display: flex;
  align-items: center;
  gap: 7px;
}

.app-shell {
  display: grid;
  gap: 20px;
  grid-template-columns: minmax(0, 1fr) minmax(330px, 390px);
  padding: 0 clamp(18px, 4vw, 48px) 32px;
}

.stage-panel {
  min-width: 0;
}

.stage-toolbar {
  justify-content: space-between;
  margin: 0 0 10px;
}

.pill,
.panel-heading span,
.mini-readout span {
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  display: inline-flex;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  padding: 7px 10px;
  white-space: nowrap;
}

.status-pill {
  color: var(--accent);
}

.stage-shell {
  background-color: #c7cec2;
  background-image:
    linear-gradient(45deg, rgba(255, 255, 255, 0.55) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.55) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255, 255, 255, 0.55) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255, 255, 255, 0.55) 75%);
  background-position:
    0 0,
    0 12px,
    12px -12px,
    -12px 0;
  background-size: 24px 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
  padding: 10px;
}

.stage {
  aspect-ratio: 16 / 9;
  background-color: transparent;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  border-radius: 6px;
  overflow: hidden;
  position: relative;
  width: 100%;
}

.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.stage[data-background-mode="image"]::before,
.stage[data-background-mode="color"]::before {
  background: rgba(0, 0, 0, 0.12);
}

.board-grid {
  --board-gap: 8px;
  display: grid;
  gap: var(--board-gap);
  grid-template-areas:
    "top top top"
    "left center right"
    "bottom bottom bottom";
  grid-template-columns: var(--tile-size) 1fr var(--tile-size);
  grid-template-rows: var(--tile-size) minmax(0, 1fr) var(--tile-size);
  inset: 5.4% 4.5%;
  position: absolute;
}

.side-row,
.side-column {
  display: grid;
  gap: var(--board-gap);
  min-height: 0;
  min-width: 0;
}

.side-top {
  grid-area: top;
}

.side-bottom {
  grid-area: bottom;
}

.side-left {
  grid-area: left;
}

.side-right {
  grid-area: right;
}

.center-hud {
  align-items: center;
  display: flex;
  grid-area: center;
  justify-content: center;
  overflow: hidden;
  min-width: 0;
}

.hud-card {
  align-items: center;
  background: transparent;
  border: 0;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 100%;
  min-width: min(360px, 66%);
  overflow: hidden;
  padding: clamp(12px, 1.7vw, 20px);
  text-align: center;
}

.round-label {
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.9);
}

.hud-card h2 {
  font-size: clamp(25px, 2.8vw, 38px);
  line-height: 1;
  text-shadow:
    0 2px 3px rgba(255, 255, 255, 0.9),
    0 0 12px rgba(255, 255, 255, 0.78);
}

.hud-card > p:not(.round-label):not(.roll-readout) {
  color: #43483f;
  font-size: clamp(14px, 1.4vw, 20px);
  font-weight: 700;
  min-height: 1.2em;
  text-shadow: 0 1px 3px rgba(255, 255, 255, 0.92);
}

.roll-readout {
  color: var(--muted);
  font-size: clamp(12px, 1.05vw, 16px);
  font-weight: 700;
  min-height: 1.2em;
  text-shadow: 0 1px 3px rgba(255, 255, 255, 0.92);
}

.stage-roll {
  font-size: clamp(17px, 1.6vw, 22px);
  font-weight: 900;
  min-height: 42px;
  min-width: min(260px, 86%);
}

.board-cell {
  align-items: stretch;
  background: var(--cell-color, #fff);
  border: 2px solid rgba(255, 255, 255, 0.72);
  border-radius: 8px;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.18);
  color: var(--cell-ink, #141414);
  cursor: pointer;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  padding: 7px;
  position: relative;
}

.board-cell.is-selected {
  outline: 4px solid rgba(17, 124, 111, 0.52);
  outline-offset: -4px;
}

.board-cell.is-active {
  border-color: rgba(255, 255, 255, 1);
  box-shadow:
    0 0 0 4px rgba(255, 255, 255, 0.42),
    0 14px 28px rgba(0, 0, 0, 0.26);
}

.cell-index {
  font-size: clamp(9px, 0.8vw, 12px);
  font-weight: 900;
  opacity: 0.7;
}

.cell-title {
  align-items: center;
  display: flex;
  font-size: clamp(11px, 1.05vw, 17px);
  font-weight: 900;
  justify-content: center;
  line-height: 1.15;
  min-height: 0;
  overflow-wrap: anywhere;
  text-align: center;
}

.cell-markers {
  align-items: center;
  display: flex;
  gap: 3px;
  justify-content: center;
  min-height: 12px;
}

.player-dot {
  background: var(--player-color);
  border: 2px solid rgba(255, 255, 255, 0.92);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  height: clamp(10px, 1vw, 16px);
  width: clamp(10px, 1vw, 16px);
}

.dice-tray {
  display: flex;
  gap: 10px;
  min-height: 54px;
}

.die {
  --pip: #151515;
  background: #fff;
  border: 2px solid rgba(0, 0, 0, 0.12);
  border-radius: 8px;
  box-shadow:
    inset 0 -8px 18px rgba(0, 0, 0, 0.07),
    0 12px 26px rgba(0, 0, 0, 0.2);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  height: clamp(44px, 4vw, 56px);
  padding: 10%;
  width: clamp(44px, 4vw, 56px);
}

.die.is-rolling {
  animation: dice-bounce 130ms infinite alternate;
}

.pip {
  align-self: center;
  background: var(--pip);
  border-radius: 50%;
  height: 68%;
  justify-self: center;
  width: 68%;
}

.pip.pos-1 {
  grid-area: 1 / 1;
}

.pip.pos-2 {
  grid-area: 1 / 2;
}

.pip.pos-3 {
  grid-area: 1 / 3;
}

.pip.pos-4 {
  grid-area: 2 / 1;
}

.pip.pos-5 {
  grid-area: 2 / 2;
}

.pip.pos-6 {
  grid-area: 2 / 3;
}

.pip.pos-7 {
  grid-area: 3 / 1;
}

.pip.pos-8 {
  grid-area: 3 / 2;
}

.pip.pos-9 {
  grid-area: 3 / 3;
}

@keyframes dice-bounce {
  from {
    transform: rotate(-7deg) translateY(0);
  }
  to {
    transform: rotate(7deg) translateY(-4px);
  }
}

.control-panel {
  align-self: start;
  display: grid;
  gap: 14px;
  max-height: calc(100vh - 110px);
  overflow: auto;
  padding: 2px 2px 10px;
}

.panel-section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 10px 32px rgba(25, 29, 24, 0.07);
  display: grid;
  gap: 12px;
  padding: 16px;
}

.panel-heading {
  align-items: center;
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.panel-heading h2 {
  font-size: 17px;
  line-height: 1;
}

.field {
  display: grid;
  gap: 7px;
}

.field span,
.toggle-line span {
  color: #4e534b;
  font-size: 13px;
  font-weight: 800;
}

input,
select {
  background: #fbfcfa;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  min-height: 40px;
  padding: 0 11px;
  width: 100%;
}

input[type="color"] {
  padding: 4px;
}

.color-field {
  grid-template-columns: minmax(80px, 1fr) 70px;
  align-items: end;
}

.color-field span {
  grid-column: 1 / -1;
}

.segmented {
  background: #eef2eb;
  border: 1px solid #dce3d7;
  border-radius: 8px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 4px;
}

.segmented button {
  background: transparent;
  border-color: transparent;
  min-height: 36px;
  padding: 0 8px;
}

.segmented button.is-active {
  background: #fff;
  border-color: #fff;
  box-shadow: 0 5px 16px rgba(25, 29, 24, 0.1);
  color: var(--accent-strong);
  font-weight: 900;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.wide-button {
  width: 100%;
}

.toggle-line {
  align-items: center;
  display: flex;
  gap: 10px;
}

.toggle-line input {
  accent-color: var(--accent);
  height: 18px;
  min-height: auto;
  width: 18px;
}

.cell-editor-list,
.players-list {
  display: grid;
  gap: 8px;
  max-height: 280px;
  overflow: auto;
  padding-right: 4px;
}

.bili-stats {
  border-bottom: 1px solid var(--line);
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 10px 0;
}

.bili-stats div {
  align-items: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.bili-stats div + div {
  border-left: 1px solid var(--line);
}

.bili-stats strong {
  font-size: 20px;
  line-height: 1;
}

.bili-stats span,
.service-hint {
  color: var(--muted);
  font-size: 12px;
}

.connection-badge[data-state="connecting"] {
  color: var(--gold);
}

.connection-badge[data-state="connected"] {
  color: var(--accent);
}

.connection-badge[data-state="error"] {
  color: var(--coral);
}

.bili-event-list {
  display: grid;
  gap: 6px;
  max-height: 164px;
  min-height: 40px;
  overflow: auto;
}

.bili-event,
.empty-event {
  color: #3f443d;
  font-size: 12px;
  line-height: 1.45;
  margin: 0;
}

.bili-event {
  border-left: 3px solid var(--event-color, var(--accent));
  padding-left: 8px;
}

.bili-event time {
  color: var(--muted);
  margin-right: 6px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
}

.cell-editor-row,
.player-row {
  align-items: center;
  background: #f7f8f5;
  border: 1px solid #e0e6db;
  border-radius: 8px;
  display: grid;
  gap: 8px;
  grid-template-columns: 46px 1fr 48px;
  padding: 8px;
}

.cell-editor-row button,
.player-row button {
  min-height: 34px;
  padding: 0 8px;
}

.cell-editor-row input[type="color"],
.player-row input[type="color"] {
  height: 34px;
  min-height: 34px;
}

.player-row {
  grid-template-columns: 34px 1fr 48px;
}

.player-row.is-active {
  border-color: rgba(17, 124, 111, 0.44);
  box-shadow: 0 0 0 3px rgba(17, 124, 111, 0.1);
}

.player-selector {
  border-radius: 50%;
  font-weight: 900;
  min-height: 34px;
  padding: 0;
}

.toast {
  background: rgba(21, 21, 21, 0.92);
  border-radius: 8px;
  bottom: 20px;
  color: #fff;
  font-weight: 800;
  left: 50%;
  opacity: 0;
  padding: 12px 16px;
  pointer-events: none;
  position: fixed;
  transform: translate(-50%, 12px);
  transition:
    opacity 160ms ease,
    transform 160ms ease;
  z-index: 20;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

html[data-view="overlay"],
html[data-view="overlay"] body {
  background: transparent;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
}

html[data-view="overlay"] [data-control] {
  display: none;
}

html[data-view="overlay"] .app-shell {
  display: block;
  height: 100%;
  padding: 0;
}

html[data-view="overlay"] .stage-panel,
html[data-view="overlay"] .stage-shell {
  height: 100%;
}

html[data-view="overlay"] .stage-shell {
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  padding: 0;
}

html[data-view="overlay"] .stage {
  border-radius: 0;
  height: 100vh;
  margin: 0 auto;
  max-height: 100vh;
  max-width: 100vw;
  width: min(100vw, calc(100vh * 16 / 9));
}

html[data-view="overlay"] .board-grid {
  --tile-size: clamp(70px, 5.2vw, 104px);
  inset: 5.2% 4%;
}

@media (max-width: 1320px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .control-panel {
    max-height: none;
  }
}

@media (max-width: 680px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions,
  .stage-toolbar {
    align-items: stretch;
    flex-direction: column;
    width: 100%;
  }

  .topbar-actions > *,
  .stage-toolbar > * {
    width: 100%;
  }

  .mini-readout {
    justify-content: space-between;
  }

  .board-grid {
    --board-gap: 5px;
    --tile-size: clamp(42px, 12vw, 58px);
    inset: 4.5% 3.5%;
  }

  .hud-card {
    gap: 8px;
    min-width: 68%;
    padding: 14px;
  }

  .stage-roll {
    min-height: 42px;
  }

  .cell-title {
    font-size: 10px;
  }

  .cell-index {
    font-size: 8px;
  }

  .button-row {
    grid-template-columns: 1fr;
  }
}
