:root {
  --green-dark: #2d5016;
  --green-mid: #3a6b1e;
  --green-light: #4a8528;
  --tan: #f5f0e1;
  --tan-dark: #e8e0cc;
  --gold: #c9a227;
  --gold-light: #dbb840;
  --white: #ffffff;
  --gray-100: #f7f7f7;
  --gray-200: #e5e5e5;
  --gray-400: #999;
  --gray-600: #666;
  --gray-800: #333;
  --red: #d32f2f;
  --blue: #1565c0;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.12);
  --shadow-lg: 0 4px 20px rgba(0,0,0,0.18);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--tan);
  color: var(--gray-800);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  outline: none;
  -webkit-appearance: none;
}

input {
  font-family: inherit;
  outline: none;
  -webkit-appearance: none;
}

.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 100dvh;
}
.screen.active {
  display: flex;
}

/* Header */
header {
  background: var(--green-dark);
  color: var(--white);
  padding: 16px 20px;
  padding-top: max(16px, calc(env(safe-area-inset-top, 0px) + 8px));
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}
header h1 {
  font-size: 20px;
  font-weight: 700;
  flex: 1;
}
header .back-btn {
  background: none;
  color: var(--white);
  font-size: 24px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
header .back-btn:active { background: rgba(255,255,255,0.15); }
header .header-action {
  background: rgba(255,255,255,0.15);
  color: var(--white);
  font-size: 14px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
header .header-action:active { background: rgba(255,255,255,0.25); }

/* Home Screen */
.home-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 24px 24px;
  gap: 32px;
}
.home-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.home-logo svg { width: 80px; height: 80px; }
.home-logo h2 {
  font-size: 32px;
  font-weight: 800;
  color: var(--green-dark);
}
.home-logo p {
  color: var(--gray-600);
  font-size: 14px;
}

.setup-form {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}
.player-count-row {
  display: flex;
  gap: 8px;
}
.player-count-btn {
  flex: 1;
  padding: 12px;
  font-size: 18px;
  font-weight: 700;
  background: var(--white);
  color: var(--gray-800);
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-sm);
  transition: all 0.15s;
}
.player-count-btn.selected {
  background: var(--green-dark);
  color: var(--white);
  border-color: var(--green-dark);
}
.player-count-btn:active {
  transform: scale(0.95);
}

.player-names {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.player-name-input {
  width: 100%;
  padding: 14px 16px;
  font-size: 16px;
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: var(--white);
  transition: border-color 0.15s;
}
.player-name-input:focus {
  border-color: var(--green-mid);
}

.btn-primary {
  width: 100%;
  padding: 16px;
  font-size: 17px;
  font-weight: 700;
  color: var(--white);
  background: var(--green-dark);
  border-radius: var(--radius);
  transition: all 0.15s;
  box-shadow: var(--shadow);
}
.btn-primary:active {
  transform: scale(0.97);
  background: var(--green-mid);
}
.btn-primary:disabled {
  opacity: 0.5;
  pointer-events: none;
}

.btn-secondary {
  width: 100%;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--green-dark);
  background: var(--white);
  border: 2px solid var(--green-dark);
  border-radius: var(--radius);
  transition: all 0.15s;
}
.btn-secondary:active {
  transform: scale(0.97);
  background: var(--tan);
}

.resume-banner {
  width: 100%;
  max-width: 400px;
  background: var(--white);
  border: 2px solid var(--gold);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: var(--shadow);
}
.resume-banner p {
  font-size: 14px;
  color: var(--gray-600);
}
.resume-banner strong {
  color: var(--gray-800);
}
.resume-btns {
  display: flex;
  gap: 10px;
}
.resume-btns button {
  flex: 1;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  border-radius: var(--radius-sm);
}
.btn-resume {
  background: var(--gold);
  color: var(--white);
}
.btn-resume:active { background: var(--gold-light); }
.btn-discard {
  background: var(--gray-200);
  color: var(--gray-600);
}

/* Scorecard Screen */
.scorecard-wrap {
  flex: 1;
  padding: 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.scorecard {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--white);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.scorecard th, .scorecard td {
  padding: 12px 10px;
  text-align: center;
  font-size: 15px;
  border-bottom: 1px solid var(--gray-200);
  border-right: 1px solid var(--gray-200);
}
.scorecard th:last-child, .scorecard td:last-child {
  border-right: none;
}
.scorecard tr:last-child td { border-bottom: none; }

.scorecard thead th {
  background: var(--green-dark);
  color: var(--white);
  font-weight: 700;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  position: sticky;
  top: 0;
  z-index: 2;
}
.scorecard thead th:first-child {
  min-width: 56px;
}
.scorecard thead th.leader-col {
  background: var(--green-mid);
}

/* Hole number column */
.scorecard .hole-num {
  font-weight: 700;
  color: var(--gray-600);
  background: var(--gray-100);
  min-width: 56px;
}

/* Current hole row highlight */
.scorecard .current-hole-row td {
  background: #fffde7;
}
.scorecard .current-hole-row .hole-num {
  background: var(--gold);
  color: var(--green-dark);
  font-weight: 800;
}

.scorecard .score-cell {
  cursor: pointer;
  transition: all 0.12s;
  min-width: 44px;
  min-height: 44px;
  position: relative;
}
.scorecard .score-cell:active {
  background: var(--tan);
  transform: scale(0.93);
}
.scorecard .score-cell.has-score {
  font-weight: 700;
  color: var(--gray-800);
}
.scorecard .score-cell.negative {
  color: var(--blue);
}
.scorecard .score-cell.empty {
  color: var(--gray-400);
}

/* Total row */
.scorecard .total-row td {
  border-top: 2px solid var(--green-dark);
}
.scorecard .total-cell {
  font-weight: 800;
  font-size: 17px;
  background: var(--tan);
}
.scorecard .leader-total {
  color: var(--gold);
  position: relative;
}
.scorecard .leader-total::after {
  content: ' \1F3C6';
  font-size: 14px;
}

.scorecard-footer {
  padding: 16px;
  display: flex;
  gap: 10px;
}
.scorecard-footer .btn-primary {
  flex: 1;
}

/* Score Entry Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.2s;
}
.modal-overlay.active {
  display: flex;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-sheet {
  background: var(--white);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 500px;
  max-height: 85dvh;
  overflow-y: auto;
  animation: slideUp 0.25s ease-out;
  padding: 0 0 env(safe-area-inset-bottom, 20px);
}
.modal-handle {
  width: 40px;
  height: 4px;
  background: var(--gray-400);
  border-radius: 2px;
  margin: 10px auto 0;
}
.modal-header {
  padding: 16px 20px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h3 {
  font-size: 18px;
  font-weight: 700;
}
.modal-close {
  background: var(--gray-200);
  color: var(--gray-600);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-body { padding: 0 20px 20px; }

/* Mode Toggle */
.mode-toggle {
  display: flex;
  background: var(--gray-200);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 20px;
}
.mode-toggle button {
  flex: 1;
  padding: 10px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 6px;
  background: transparent;
  color: var(--gray-600);
  transition: all 0.15s;
}
.mode-toggle button.active {
  background: var(--white);
  color: var(--gray-800);
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

/* Quick Total Input */
.quick-total {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.quick-total-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.score-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.score-sign {
  position: absolute;
  left: 12px;
  font-size: 28px;
  font-weight: 800;
  color: var(--blue);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}
.score-sign.visible {
  opacity: 1;
}
.quick-total input {
  width: 120px;
  height: 64px;
  text-align: center;
  font-size: 32px;
  font-weight: 700;
  border: 3px solid var(--gray-200);
  border-radius: var(--radius);
  background: var(--white);
  transition: all 0.15s;
}
.quick-total input.negative-mode {
  border-color: var(--blue);
  padding-left: 28px;
}
.quick-total input:focus {
  border-color: var(--green-mid);
}
.quick-total input.negative-mode:focus {
  border-color: var(--blue);
}
.quick-total .neg-toggle {
  width: 64px;
  height: 64px;
  border-radius: var(--radius);
  font-size: 28px;
  font-weight: 800;
  background: var(--gray-200);
  color: var(--gray-600);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
}
.quick-total .neg-toggle.active {
  background: var(--blue);
  color: var(--white);
}
.quick-total .neg-toggle:active {
  transform: scale(0.9);
}

/* Card Grid */
.card-grid-entry { display: flex; flex-direction: column; gap: 16px; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.card-slot {
  aspect-ratio: 3/4;
  background: var(--gray-100);
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
  min-height: 56px;
}
.card-slot:active { transform: scale(0.93); }
.card-slot.filled {
  background: var(--white);
  border-color: var(--green-mid);
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.card-slot.matched {
  border-color: var(--gold);
  background: #fffde7;
}
.card-slot.matched .card-value {
  text-decoration: line-through;
  color: var(--gray-400);
}
.card-slot.matched::after {
  content: '0';
  position: absolute;
  font-size: 14px;
  font-weight: 700;
  color: var(--gold);
  bottom: 4px;
  right: 6px;
}
.card-slot.negative .card-value { color: var(--blue); }
.card-slot .card-placeholder {
  color: var(--gray-400);
  font-size: 24px;
}

.card-grid-total {
  text-align: center;
  font-size: 15px;
  color: var(--gray-600);
}
.card-grid-total strong {
  font-size: 24px;
  color: var(--gray-800);
  margin-left: 4px;
}

/* Card Picker */
.card-picker-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 300;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.15s;
}
.card-picker-overlay.active { display: flex; }

.card-picker {
  background: var(--white);
  border-radius: var(--radius);
  padding: 20px;
  width: 90%;
  max-width: 360px;
  box-shadow: var(--shadow-lg);
}
.card-picker h4 {
  text-align: center;
  margin-bottom: 14px;
  font-size: 16px;
  color: var(--gray-600);
}
.card-picker-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.card-pick-btn {
  padding: 12px 4px;
  font-size: 18px;
  font-weight: 700;
  background: var(--gray-100);
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-sm);
  color: var(--gray-800);
  transition: all 0.12s;
  min-height: 48px;
}
.card-pick-btn:active {
  transform: scale(0.9);
  background: var(--green-dark);
  color: var(--white);
  border-color: var(--green-dark);
}
.card-pick-btn.negative { color: var(--blue); }
.card-picker-cancel {
  width: 100%;
  padding: 12px;
  margin-top: 12px;
  font-size: 15px;
  font-weight: 600;
  background: var(--gray-200);
  color: var(--gray-600);
  border-radius: var(--radius-sm);
}

.modal-save {
  width: 100%;
  padding: 16px;
  font-size: 17px;
  font-weight: 700;
  background: var(--green-dark);
  color: var(--white);
  border-radius: var(--radius);
  margin-top: 16px;
  transition: all 0.15s;
}
.modal-save:active {
  transform: scale(0.97);
  background: var(--green-mid);
}

/* Summary Screen */
.summary-content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
}

.winner-card {
  text-align: center;
  padding: 24px;
  background: linear-gradient(135deg, var(--green-dark), var(--green-mid));
  color: var(--white);
  border-radius: var(--radius);
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
}
.winner-trophy { font-size: 56px; margin-bottom: 8px; }
.winner-name {
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 4px;
}
.winner-score { font-size: 16px; opacity: 0.85; }

.celebration {
  animation: celebrate 0.6s ease-out;
}
@keyframes celebrate {
  0% { transform: scale(0.5); opacity: 0; }
  60% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

.stats-table {
  width: 100%;
  max-width: 500px;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--white);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.stats-table th, .stats-table td {
  padding: 10px 12px;
  text-align: center;
  font-size: 14px;
  border-bottom: 1px solid var(--gray-200);
}
.stats-table th {
  background: var(--green-dark);
  color: var(--white);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.stats-table th:first-child, .stats-table td:first-child {
  text-align: left;
  padding-left: 14px;
}
.stats-table tr:last-child td { border-bottom: none; }

.summary-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 400px;
}

/* History Screen */
.history-content {
  flex: 1;
  padding: 16px;
}
.history-empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--gray-400);
  font-size: 15px;
}
.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.history-card {
  background: var(--white);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.12s;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.history-card:active { transform: scale(0.98); }
.history-info h4 { font-size: 15px; font-weight: 700; margin-bottom: 4px; }
.history-info p { font-size: 13px; color: var(--gray-600); }
.history-meta {
  text-align: right;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}
.history-date { font-size: 12px; color: var(--gray-400); }
.history-delete {
  background: none;
  color: var(--red);
  font-size: 13px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 4px;
}
.history-delete:active { background: #ffebee; }

/* History Detail Scorecard */
.history-detail-content {
  flex: 1;
  padding: 16px;
  overflow-x: auto;
}

/* Utilities */
.hidden { display: none !important; }

/* Confetti container */
.confetti-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 999;
  overflow: hidden;
}
.confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  top: -10px;
  animation: confettiFall 2.5s linear forwards;
}
@keyframes confettiFall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* Landscape / wider screens */
@media (min-width: 768px) {
  .scorecard { min-width: unset; }
  .modal-sheet {
    border-radius: var(--radius);
    margin-bottom: 20px;
    max-height: 80vh;
  }
  .modal-overlay { align-items: center; }
}
