:root {
  --bg-1: #f8f4e8;
  --bg-2: #f2e8cf;
  --ink: #1d1b1a;
  --muted: #6a6259;
  --card: rgba(255, 253, 246, 0.85);
  --line: rgba(50, 42, 30, 0.2);
  --accent: #0f766e;
  --accent-strong: #115e59;
  --danger: #b91c1c;
  --danger-strong: #991b1b;
  --gold: #b88900;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family:
    system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    Helvetica,
    Arial,
    sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 15%, #fff9de 0, transparent 35%), linear-gradient(140deg, var(--bg-1), var(--bg-2));
  min-height: 100vh;
  padding: 1.2rem;
  position: relative;
}

.bg-grid {
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(to right, rgba(0, 0, 0, 0.03) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0, 0, 0, 0.03) 1px, transparent 1px);
  background-size: 24px 24px;
  pointer-events: none;
}

.bg-glow {
  position: fixed;
  inset: auto -10% -25% auto;
  width: 420px;
  height: 420px;
  background: radial-gradient(circle, rgba(15, 118, 110, 0.25) 0, rgba(15, 118, 110, 0) 70%);
  pointer-events: none;
}

h1,
h2,
h3 {
  margin: 0;
  font-family:
    system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    Helvetica,
    Arial,
    sans-serif;
  font-weight: 900;
  letter-spacing: 0.02em;
}

.app-title {
  color: var(--accent-strong);
  line-height: 1.1;
}

.app-header {
  max-width: 1200px;
  margin: 0 auto 1rem;
  display: flex;
  gap: 1rem;
  align-items: center;
  justify-content: flex-start;
}

.layout {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 1rem;
}

.app-footer {
  max-width: 1200px;
  margin: 1rem auto 0;
  padding: 0 0.2rem 0.2rem;
  color: var(--muted);
  font-size: 0.7rem;
  opacity: 0.78;
  text-align: right;
}

.app-footer p {
  margin: 0;
}

.app-footer p + p {
  margin-top: 0.2rem;
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  color: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  border-radius: 16px;
  box-shadow: 0 12px 28px rgba(23, 19, 13, 0.08);
  padding: 1rem;
  animation: reveal 420ms ease both;
}

#playersCard {
  grid-column: span 4;
}

#roundsCard {
  grid-column: span 8;
}

#rankingCard,
#chartCard,
#winsCard {
  grid-column: span 6;
}

.card-head p {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.row-between {
  display: flex;
  gap: 0.8rem;
  justify-content: space-between;
  align-items: flex-start;
}

.inline-form {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.8rem;
}

.players-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  width: 100%;
  cursor: default;
  list-style: none;
}

.players-summary::-webkit-details-marker {
  display: none;
}

.players-summary::marker {
  content: '';
}

.players-chevron {
  flex: 0 0 auto;
  width: 1.9rem;
  height: 1.9rem;
  transition:
    transform 140ms ease,
    box-shadow 140ms ease,
    background-color 140ms ease;
  border-radius: 999px;
  background: var(--accent-strong);
  box-shadow: 0 0 0 1px rgba(15, 94, 89, 0.5);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.players-chevron::before {
  content: '';
  width: 0.54rem;
  height: 0.54rem;
  border-right: 2.5px solid #f3fffd;
  border-bottom: 2.5px solid #f3fffd;
  transform: rotate(45deg) translateY(-1px);
  transition: transform 140ms ease;
}

.players-details:not([open]) .players-chevron::before {
  transform: rotate(-45deg) translateY(0);
}

.players-description {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

@media (min-width: 641px) {
  .players-summary {
    pointer-events: none;
  }

  .players-chevron {
    display: none;
  }
}

input,
button {
  font: inherit;
}

input[type='text'],
input[type='number'] {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem 0.65rem;
  background: #fffef9;
}

input[type='number'] {
  appearance: textfield;
  -moz-appearance: textfield;
}

input[type='number']::-webkit-outer-spin-button,
input[type='number']::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type='text'] {
  width: 100%;
}

.btn {
  border: 0;
  border-radius: 10px;
  padding: 0.55rem 0.85rem;
  background: var(--accent);
  color: #f9fffe;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 140ms ease,
    background-color 140ms ease;
}

.btn:hover {
  transform: translateY(-1px);
  background: var(--accent-strong);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
}

.btn-secondary {
  background: #1f2937;
}

.btn-secondary:hover {
  background: #111827;
}

.btn-danger {
  background: var(--danger);
}

.btn-danger:hover {
  background: var(--danger-strong);
}

.tag-list {
  margin: 0.8rem 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.tag-list li {
  display: inline-flex;
  gap: 0.4rem;
  align-items: center;
  background: #fffbef;
  border: 1px solid #eadfca;
  border-radius: 999px;
  padding: 0.25rem 0.6rem;
}

.player-name {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-actions {
  display: inline-flex;
  gap: 0.3rem;
}

.player-chip-btn {
  border: 0;
  border-radius: 999px;
  min-width: 26px;
  height: 26px;
  padding: 0 0.45rem;
  font-weight: 700;
  cursor: pointer;
}

.edit-player {
  background: #dff4ef;
  color: #0f4e49;
}

.remove-player {
  background: #f9d5d5;
  color: #7f1d1d;
}

.table-wrap {
  overflow-x: auto;
  margin-top: 0.8rem;
}

.rounds-actions {
  margin-top: 0.7rem;
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
}

.rounds-action-btn {
  border: 0;
  border-radius: 10px;
  padding: 0.48rem 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 140ms ease,
    background-color 140ms ease,
    box-shadow 140ms ease;
}

.rounds-action-btn:hover {
  transform: translateY(-1px);
}

.rounds-action-btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.rounds-action-btn-secondary {
  background: #dff4ef;
  color: #0f4e49;
}

.rounds-action-btn-secondary:hover {
  background: #ccece5;
}

.rounds-action-btn-danger {
  background: var(--danger);
  color: #fff5f5;
}

.rounds-action-btn-danger:hover {
  background: var(--danger-strong);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 480px;
}

th,
td {
  border: 1px solid var(--line);
  padding: 0.45rem;
  text-align: center;
}

th {
  background: #f9f3e2;
}

.action-col {
  width: 74px;
  text-align: center;
}

th.action-col,
td.action-col {
  padding-right: 0.35rem;
  padding-left: 0.35rem;
}

tfoot td {
  background: #f4ebd4;
  font-weight: 700;
}

.footer-rank-row td {
  background: #efe4c6;
}

td:first-child,
th:first-child {
  min-width: 66px;
  text-align: left;
  font-weight: 700;
  position: sticky;
  left: 0;
  background: #fdf8ea;
}

.points-input {
  width: 62px;
  min-width: 62px;
}

.round-cell-end {
  display: flex;
  justify-content: center;
}

.table-action-btn {
  border: 0;
  border-radius: 999px;
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: 800;
}

.delete-round-btn {
  background: #f9d5d5;
  color: #7f1d1d;
}

.add-round-btn {
  background: #dff4ef;
  color: #0f4e49;
}

.restart-round-btn {
  background: var(--danger);
  color: #fff5f5;
  animation:
    restart-pop 500ms cubic-bezier(0.34, 1.56, 0.64, 1) both,
    restart-pulse 2s ease-in-out 500ms 3;
}

.restart-round-btn:hover {
  background: var(--danger-strong);
}

.restart-round-btn--static {
  animation: none;
}

.restart-round-btn--static:hover {
  animation: none;
}

@keyframes restart-pop {
  from {
    opacity: 0;
    transform: scale(0.4);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes restart-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(185, 28, 28, 0);
  }
  50% {
    box-shadow: 0 0 0 5px rgba(185, 28, 28, 0.4);
  }
}

.table-rank {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: #fff7dc;
  border: 1px solid #e5cf8e;
}

.winner-column-head {
  background: #f4e4aa;
  color: #5f4511;
}

.winner-column-cell {
  background: #fff6da;
}

.winner-column-cell .points-input {
  border-color: #d1aa4a;
  background: #fffaf0;
}

.winner-total-cell {
  font-weight: 800;
  color: #5f4511;
}

.winner-rank-badge {
  background: #ffe7a0;
  border-color: #d3a73c;
  color: #5f4511;
}

.ranking-list {
  margin: 0.85rem 0;
  padding-left: 1.25rem;
  display: grid;
  gap: 0.45rem;
}

.ranking-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  background: #fff9ea;
  border: 1px solid #f0e2bd;
  border-radius: 10px;
  padding: 0.4rem 0.55rem;
}

.ranking-list .top {
  border-color: #e9c95f;
  background: #fff3c8;
}

.ranking-actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.chart-wrap {
  position: relative;
  height: 260px;
  min-height: 220px;
  margin-top: 0.8rem;
}

#scoreChart {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.wins-list {
  margin-top: 0.8rem;
  display: grid;
  gap: 0.45rem;
}

.win-row {
  display: flex;
  justify-content: space-between;
  border: 1px solid #eadfc5;
  border-radius: 10px;
  padding: 0.4rem 0.6rem;
  background: #fff8e7;
}

.side-panel-actions {
  margin-top: 0.8rem;
  display: flex;
  flex-direction: row;
  gap: 0.5rem;
  flex-wrap: wrap;
}

dialog {
  border: 1px solid #d8c8aa;
  border-radius: 14px;
  width: min(520px, 92vw);
}

dialog::backdrop {
  background: rgba(25, 20, 16, 0.44);
}

.popup-dialog {
  width: min(460px, 92vw);
  border: 1px solid #d8c8aa;
  border-radius: 18px;
  box-shadow: 0 20px 44px rgba(25, 20, 16, 0.2);
}

.popup-content {
  background: linear-gradient(180deg, #fffdf6, #fff7e5);
}

.popup-eyebrow {
  margin: 0 0 0.2rem;
  color: var(--accent-strong);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.72rem;
}

.popup-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0;
  margin: 1rem 0 0;
}

.popup-actions-stack {
  display: block;
}

.popup-choice-row {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

.popup-top-action {
  display: block;
  margin-left: auto;
}

.popup-top-action[hidden] {
  display: none !important;
}

.popup-choice-row,
.popup-top-action {
  margin-top: 0.6rem;
}

#renamePlayerInput {
  width: 100%;
  margin-top: 0.85rem;
}

.legal-dialog {
  width: min(560px, 92vw);
}

.legal-scroll {
  max-height: min(60vh, 420px);
  overflow-y: auto;
  margin-top: 0.6rem;
  padding-right: 0.4rem;
}

.legal-scroll section + section {
  margin-top: 1rem;
}

.legal-scroll h4 {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
  color: var(--accent-strong);
}

.legal-scroll p {
  margin: 0 0 0.5rem;
  line-height: 1.4;
}

.legal-scroll p:last-child {
  margin-bottom: 0;
}

.legal-scroll a {
  color: inherit;
}

.dialog-content {
  margin: 0;
  padding: 1rem;
}

.dialog-content p {
  color: var(--muted);
}

.dialog-content menu {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0;
  margin: 0.9rem 0 0;
}

.dialog-content menu.popup-actions-stack {
  display: block;
}

@keyframes reveal {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 1000px) {
  #playersCard,
  #roundsCard,
  #rankingCard,
  #chartCard,
  #winsCard {
    grid-column: span 12;
  }
}

@media (max-width: 820px) {
  body {
    padding: 0.9rem;
  }

  .layout {
    gap: 0.8rem;
  }

  .card {
    padding: 0.85rem;
    border-radius: 14px;
  }

  .app-header {
    margin-bottom: 0.8rem;
  }

  .app-title {
    font-size: 1.65rem;
  }

  .card-head p {
    font-size: 0.84rem;
  }

  .btn {
    padding: 0.5rem 0.7rem;
  }

  .tag-list {
    gap: 0.35rem;
  }

  .tag-list li {
    padding: 0.2rem 0.5rem;
  }

  .player-name {
    max-width: 140px;
  }

  table {
    min-width: 0;
  }

  th,
  td {
    padding: 0.38rem;
    font-size: 0.9rem;
  }

  .points-input {
    width: 72px;
    min-width: 50px;
    padding: 0.45rem 0.45rem;
  }

  .action-col {
    width: 58px;
  }

  .ranking-list {
    margin: 0.7rem 0;
    gap: 0.35rem;
  }

  .ranking-list li {
    padding: 0.35rem 0.5rem;
  }

  .wins-list {
    gap: 0.35rem;
  }

  .win-row {
    padding: 0.35rem 0.5rem;
  }

  .chart-wrap {
    height: 220px;
    min-height: 190px;
  }

  .popup-dialog {
    width: min(440px, 95vw);
    border-radius: 14px;
  }

  .dialog-content {
    padding: 0.85rem;
  }
}

@media (max-width: 640px) {
  body {
    padding: 0.65rem;
  }

  .app-header {
    align-items: flex-start;
    margin-bottom: 0.65rem;
  }

  .app-footer {
    margin-top: 0.75rem;
    font-size: 0.64rem;
  }

  .app-title {
    font-size: 1.38rem;
  }

  .inline-form {
    flex-direction: column;
    gap: 0.4rem;
    margin-top: 0.6rem;
  }

  .players-summary {
    cursor: pointer;
  }

  .players-summary:hover .players-chevron,
  .players-summary:focus-visible .players-chevron {
    background: var(--accent);
    box-shadow: 0 0 0 3px rgba(15, 94, 89, 0.28);
    transform: translateY(-1px) scale(1.04);
  }

  .players-summary:active .players-chevron {
    transform: scale(0.98);
  }

  .players-description {
    margin-top: 0.25rem;
    line-height: 1.3;
    font-size: 0.8rem;
  }

  .row-between {
    flex-direction: column;
  }

  .ranking-actions {
    flex-direction: column;
    gap: 0.45rem;
  }

  .card {
    padding: 0.7rem;
    border-radius: 12px;
  }

  .card-head p {
    margin-top: 0.25rem;
    line-height: 1.3;
    font-size: 0.8rem;
  }

  .btn {
    width: 100%;
    padding: 0.5rem 0.6rem;
    border-radius: 9px;
    font-size: 0.92rem;
  }

  .tag-list {
    margin-top: 0.6rem;
    gap: 0.3rem;
  }

  #playersCard .tag-list {
    display: grid;
    gap: 0.35rem;
  }

  .tag-list li {
    max-width: 100%;
    padding: 0.18rem 0.45rem;
  }

  #playersCard .tag-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 9px;
    padding: 0.35rem 0.5rem;
  }

  .player-name {
    max-width: 115px;
  }

  #playersCard .player-name {
    max-width: none;
  }

  .player-chip-btn {
    min-width: 24px;
    height: 24px;
    padding: 0 0.4rem;
  }

  .table-wrap {
    margin-top: 0.65rem;
    border-radius: 0;
  }

  .rounds-actions {
    margin-top: 0.6rem;
    flex-direction: column;
    gap: 0.4rem;
  }

  .rounds-action-btn {
    width: 100%;
    font-size: 0.92rem;
    padding: 0.5rem 0.6rem;
  }

  table {
    min-width: 0;
  }

  th,
  td {
    padding: 0.24rem;
    font-size: 0.78rem;
  }

  .points-input {
    width: 50px;
    min-width: 50px;
    border-radius: 8px;
    padding: 0.26rem 0.24rem;
  }

  .action-col {
    width: 40px;
  }

  .table-action-btn {
    width: 24px;
    height: 24px;
    font-size: 0.95rem;
  }

  .table-rank {
    min-width: 28px;
    padding: 0.15rem 0.3rem;
    font-size: 0.76rem;
  }

  .ranking-list {
    padding-left: 1rem;
  }

  .ranking-list li {
    padding: 0.3rem 0.45rem;
    font-size: 0.86rem;
  }

  .win-row {
    padding: 0.3rem 0.45rem;
    font-size: 0.86rem;
  }

  .popup-choice-row {
    flex-direction: column;
    gap: 0.4rem;
  }

  .popup-top-action {
    width: 100%;
    margin-left: 0;
  }

  .popup-dialog {
    width: 96vw;
  }

  .popup-actions {
    gap: 0.4rem;
  }

  .chart-wrap {
    height: 190px;
    min-height: 170px;
  }
}

@media (max-width: 420px) {
  .app-title {
    font-size: 1.22rem;
  }

  table {
    min-width: 0;
  }

  .points-input {
    width: 50px;
    min-width: 50px;
  }

  .action-col {
    width: 36px;
  }
}
