:root {
  color-scheme: light;
  --controls-height: 250px;
  --bg: #1f4f46;
  --panel: #f7faf8;
  --panel-soft: #edf4f0;
  --table-felt: #2f6b55;
  --table-row: rgba(247, 250, 248, 0.9);
  --ink: #171b20;
  --muted: #66756e;
  --line: #c8d8d0;
  --accent: #c89b2d;
  --accent-strong: #8d6a16;
  --danger: #b3261e;
  --red: #cc3030;
  --blue: #246fca;
  --yellow: #bf8900;
  --black: #20242a;
  --joker: #7b3fc8;
  --tile-face: url("assets/tile-face.png");
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 18% 8%, rgba(255, 255, 255, 0.1), transparent 24%),
    linear-gradient(160deg, #1b443d 0%, var(--bg) 52%, #173a34 100%);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, sans-serif;
}

.iconSprite {
  display: none;
}

button,
input {
  font: inherit;
}

.app {
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  height: 100vh;
  height: 100svh;
  min-height: 100vh;
  min-height: 100svh;
  padding: 0;
}

.stickyControls {
  position: fixed;
  top: 0;
  left: 50%;
  width: min(1180px, calc(100vw - 32px));
  transform: translateX(-50%);
  z-index: 20;
  padding: 8px 0 10px;
  background: linear-gradient(180deg, var(--bg) 0%, rgba(31, 79, 70, 0.96) 86%, rgba(31, 79, 70, 0) 100%);
  overscroll-behavior: contain;
  touch-action: none;
}

.scrollArea {
  position: fixed;
  left: 50%;
  top: calc(var(--controls-height) + 10px);
  bottom: 0;
  width: min(1180px, calc(100vw - 32px));
  transform: translateX(-50%);
  overflow: auto;
  padding: 0 0 28px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.topbar,
.toolbar,
.playersSection,
.boardSection,
.rackSection,
.logSection {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(12, 20, 28, 0.12);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-top: 12px;
  padding: 18px 20px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.1;
}

h2 {
  font-size: 18px;
}

.sectionTitle p,
.log {
  color: var(--muted);
}

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.stats span {
  min-width: 86px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
  text-align: center;
  font-weight: 700;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  padding: 12px;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(12, 20, 28, 0.08);
  padding: 0 14px;
  cursor: pointer;
}

.buttonIcon {
  flex: 0 0 auto;
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button:hover {
  border-color: #b8c1ca;
  background: #edf0f3;
}

button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

button.primary:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.modalOverlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(12, 20, 28, 0.42);
}

.modalOverlay[hidden] {
  display: none;
}

.winnerDialog {
  width: min(360px, calc(100vw - 36px));
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 46px rgba(12, 20, 28, 0.26);
}

.winnerDialog h2 {
  font-size: 22px;
}

.winnerDialog p {
  margin-top: 8px;
  color: var(--muted);
}

.dialogActions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 18px;
}

.boardSection,
.playersSection,
.rackSection,
.logSection {
  margin-top: 12px;
  padding: 16px;
}

.boardSection {
  background:
    radial-gradient(circle at 22% 12%, rgba(255, 255, 255, 0.12), transparent 26%),
    radial-gradient(circle at 82% 88%, rgba(0, 0, 0, 0.1), transparent 30%),
    repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.04) 0 1px, transparent 1px 5px),
    linear-gradient(160deg, #35745b 0%, var(--table-felt) 58%, #285747 100%);
  border-color: rgba(201, 221, 211, 0.62);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.12),
    inset 0 -12px 30px rgba(12, 20, 28, 0.1);
}

.boardSection .sectionTitle h2 {
  color: #f7faf8;
  text-shadow: 0 1px 1px rgba(12, 20, 28, 0.28);
}

.sectionTitle {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.stickyControls .rackSection {
  margin-top: 0;
  padding: 10px;
}

.stickyControls .sectionTitle {
  margin-bottom: 4px;
}

.stickyControls .sectionTitle h2 {
  font-size: 17px;
}

.stickyControls .sectionTitle p {
  font-size: 13px;
}

.sets {
  display: grid;
  gap: 8px;
}

.players {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}

.playerCard {
  display: grid;
  grid-template-columns: minmax(74px, 0.7fr) minmax(104px, 0.8fr) minmax(0, 2.2fr);
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
}

.playerCard.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px rgba(31, 122, 92, 0.12);
}

.playerName {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-weight: 800;
}

.playerDetails {
  color: var(--muted);
  font-size: 14px;
}

.playerRackIcons {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: 12px;
  grid-auto-columns: 8px;
  align-items: center;
  justify-content: end;
  gap: 3px;
  min-height: 14px;
  overflow: hidden;
}

.playerMiniTile {
  width: 8px;
  height: 12px;
  border: 1px solid #7f8994;
  border-radius: 2px;
  background: linear-gradient(145deg, #ffffff 0%, #f2f4f7 100%);
  box-shadow: 0 1px 1px rgba(12, 20, 28, 0.12);
}

.playerMiniMore {
  align-self: center;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}

.setRow {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 6px;
  min-height: 58px;
  padding: 8px;
  border: 1px solid rgba(210, 226, 218, 0.82);
  border-radius: 8px;
  background: var(--table-row);
  box-shadow: 0 2px 6px rgba(12, 20, 28, 0.12);
}

.setRow.invalid {
  border-color: #e0a4a0;
  background: #fff3f2;
}

.setTiles,
.rack {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.rack {
  min-height: 80px;
  max-height: 132px;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  box-shadow: inset 0 1px 3px rgba(12, 20, 28, 0.06);
}

.tile {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 50px;
  border: 2px solid currentColor;
  border-radius: 6px;
  background-color: #fff;
  background-image: linear-gradient(145deg, rgba(255, 255, 255, 0.08), rgba(242, 244, 247, 0.1)), var(--tile-face);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-clip: padding-box;
  color: var(--black);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.95),
    inset -1px -2px 0 rgba(12, 20, 28, 0.12),
    0 2px 3px rgba(12, 20, 28, 0.18);
  font-weight: 800;
  line-height: 1;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.72);
  user-select: none;
}

.tile.red {
  color: var(--red);
}

.tile.blue {
  color: var(--blue);
}

.tile.yellow {
  color: var(--yellow);
}

.tile.black {
  color: var(--black);
}

.tile.joker {
  color: var(--joker);
  background-image: linear-gradient(145deg, rgba(255, 255, 255, 0.1), rgba(123, 63, 200, 0.1)), var(--tile-face);
}

.rack .tile,
.setTiles .tile {
  cursor: pointer;
}

.stickyControls .rack .tile {
  width: 34px;
  height: 44px;
  font-size: 15px;
}

.setTiles .tile {
  width: 34px;
  height: 44px;
  font-size: 15px;
}

.tile.selected {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  transform: translateY(-3px);
}

.tile.latestDraw {
  border-width: 4px;
  background-image: linear-gradient(145deg, rgba(255, 255, 255, 0.12), rgba(31, 122, 92, 0.14)), var(--tile-face);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.95),
    inset -1px -2px 0 rgba(12, 20, 28, 0.12),
    0 2px 3px rgba(12, 20, 28, 0.18),
    0 0 0 3px rgba(31, 122, 92, 0.18);
}

.tile.newThisTurn {
  background-image: linear-gradient(145deg, rgba(255, 255, 255, 0.12), rgba(31, 122, 92, 0.1)), var(--tile-face);
}

.setMeta {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
  min-width: 46px;
}

.setAddButton {
  min-height: 30px;
  padding: 0 8px;
  font-size: 12px;
  font-weight: 700;
}

.emptyState {
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: var(--panel-soft);
}

.logSection h2 {
  margin-bottom: 8px;
}

.log {
  display: grid;
  gap: 5px;
  max-height: 130px;
  overflow: auto;
  padding-left: 22px;
}

.log li:first-child {
  color: var(--ink);
}

@media (max-width: 720px) {
  .app {
    width: min(100vw - 20px, 1180px);
    padding-top: 0;
  }

  .stickyControls,
  .scrollArea {
    width: min(100vw - 20px, 1180px);
  }

  .sectionTitle {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .stats {
    justify-content: flex-start;
  }

  .toolbar {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    padding: 8px;
  }

  .toolbar button {
    min-height: 38px;
    gap: 4px;
    padding: 0 6px;
    font-size: 12px;
  }

  .buttonIcon {
    width: 16px;
    height: 16px;
  }

  .rack {
    max-height: 112px;
    min-height: 56px;
    padding: 8px;
  }

  .stickyControls .rack .tile {
    width: 32px;
    height: 42px;
  }

  .setTiles .tile {
    width: 32px;
    height: 42px;
  }

  .setRow {
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
    padding: 7px;
  }

  .players {
    grid-template-columns: 1fr;
  }

  .playerCard {
    grid-template-columns: minmax(58px, 0.65fr) minmax(88px, 0.9fr) minmax(0, 1.8fr);
    min-height: 38px;
    padding: 7px 8px;
    gap: 6px;
  }

  .playerDetails {
    font-size: 12px;
  }

  .playerRackIcons {
    grid-template-rows: 10px;
    grid-auto-columns: 7px;
    gap: 2px;
    min-height: 12px;
  }

  .playerMiniTile {
    width: 7px;
    height: 10px;
  }

  .setMeta {
    min-width: 36px;
    font-size: 11px;
  }

  .setAddButton {
    min-height: 28px;
    padding: 0 7px;
    font-size: 11px;
  }
}
