:root {
  --bg: #07110d;
  --bg-soft: #0b1b14;
  --surface: rgba(14, 33, 24, .88);
  --surface-solid: #10271d;
  --surface-2: #173629;
  --border: rgba(255,255,255,.09);
  --text: #f5fbf7;
  --muted: #8fa79b;
  --green: #22c97a;
  --green-dark: #139257;
  --gold: #f0c95c;
  --orange: #ef8a4b;
  --red: #ff6c6c;
  --blue: #63a8ff;
  --shadow: 0 22px 70px rgba(0,0,0,.34);
  --radius: 22px;
}

:root.light {
  --bg: #eaf1ed;
  --bg-soft: #dfeae4;
  --surface: rgba(255,255,255,.91);
  --surface-solid: #ffffff;
  --surface-2: #edf5f1;
  --border: rgba(10,45,29,.12);
  --text: #10231a;
  --muted: #63776d;
  --shadow: 0 22px 55px rgba(22,61,42,.12);
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 0%, rgba(25,121,76,.24), transparent 30%),
    radial-gradient(circle at 100% 30%, rgba(200,153,48,.12), transparent 28%),
    linear-gradient(145deg, var(--bg), var(--bg-soft));
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  overflow-x: hidden;
}
button, input, select { font: inherit; }
button { border: 0; cursor: pointer; touch-action: manipulation; }
input, select { color-scheme: dark; }
:root.light input, :root.light select { color-scheme: light; }
.hidden { display: none !important; }

.ambient { position: fixed; pointer-events: none; filter: blur(70px); opacity: .25; z-index: 0; }
.ambient-a { width: 360px; height: 360px; background: #1bcc76; left: -170px; top: 24%; border-radius: 50%; }
.ambient-b { width: 300px; height: 300px; background: #e6b33f; right: -180px; bottom: 6%; border-radius: 50%; }

.app-shell { width: min(1440px, 100%); margin: 0 auto; padding: 28px 24px 42px; position: relative; z-index: 1; }
.app-header { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 26px; }
.brand-wrap { display: flex; align-items: center; gap: 14px; }
.ball-logo { width: 58px; height: 58px; flex: 0 0 58px; display: grid; place-items: center; border-radius: 50%; background: radial-gradient(circle at 35% 28%, #fff 0 23%, #e8eee9 24% 38%, #111 39% 100%); box-shadow: 0 12px 30px rgba(0,0,0,.35); }
.ball-logo span { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: #fff; color: #111; font-size: 13px; font-weight: 1000; }
.eyebrow { margin: 0 0 4px; color: var(--green); font-size: 10px; letter-spacing: .19em; font-weight: 900; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 2px; font-size: 28px; line-height: 1; letter-spacing: -.04em; }
.brand-subtitle { margin: 0; color: var(--muted); font-size: 12px; }
.header-actions { display: flex; align-items: center; gap: 8px; }

.surface { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); backdrop-filter: blur(18px); }
.hero-card { min-height: 188px; padding: 34px 38px; margin-bottom: 18px; border-radius: 26px; display: flex; align-items: center; justify-content: space-between; gap: 24px; overflow: hidden; position: relative; background: linear-gradient(120deg, #0a3e29, #12563a 60%, #3c541d); box-shadow: 0 24px 70px rgba(0,0,0,.28); }
.hero-card::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, transparent, rgba(255,255,255,.04)); pointer-events: none; }
.hero-card h2 { font-size: clamp(28px, 5vw, 48px); margin: 12px 0 8px; letter-spacing: -.05em; }
.hero-card p { max-width: 660px; margin: 0; color: #cfe2d8; line-height: 1.7; }
.pill { display: inline-flex; align-items: center; min-height: 28px; padding: 0 11px; border: 1px solid rgba(255,255,255,.24); border-radius: 99px; color: #ecfff5; background: rgba(255,255,255,.08); font-size: 12px; font-weight: 800; }
.hero-balls { display: flex; align-items: center; margin-right: 14px; transform: rotate(-8deg); }
.hero-balls i { width: 64px; height: 64px; margin-left: -10px; display: grid; place-items: center; border-radius: 50%; color: #111; background: radial-gradient(circle at 34% 27%, #fff, #ecefe8 58%, #bbc8be); border: 5px solid var(--ball-color, #f2ca48); font-style: normal; font-weight: 1000; box-shadow: 0 14px 25px rgba(0,0,0,.25); }
.hero-balls i:nth-child(2) { --ball-color: #9d60d3; transform: translateY(12px); }
.hero-balls i:nth-child(3) { --ball-color: #8e2f32; transform: translateY(-8px); }
.hero-balls i:nth-child(4) { --ball-color: #278bd6; transform: translateY(7px); }

.setup-layout { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(320px, .75fr); gap: 18px; }
.setup-main, .setup-side { padding: 24px; }
.section-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.section-heading > div { display: flex; align-items: center; gap: 11px; }
.section-heading span { color: var(--green); font-size: 11px; font-weight: 1000; letter-spacing: .15em; }
.section-heading h3, .card-heading h3 { margin: 0; font-size: 18px; }
.separated { margin-top: 28px; padding-top: 24px; border-top: 1px solid var(--border); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.field-wide { grid-column: 1 / -1; }
.field > span { display: block; margin-bottom: 8px; color: var(--muted); font-size: 12px; font-weight: 800; }
.field input, .field select { width: 100%; min-height: 50px; padding: 10px 14px; border-radius: 13px; border: 1px solid var(--border); outline: none; background: rgba(255,255,255,.045); color: var(--text); transition: .18s ease; }
.field input:focus, .field select:focus { border-color: rgba(34,201,122,.72); box-shadow: 0 0 0 4px rgba(34,201,122,.11); }
.name-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.name-box { position: relative; }
.name-box span { position: absolute; top: 50%; left: 13px; transform: translateY(-50%); width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%; background: var(--surface-2); color: var(--green); font-size: 11px; font-weight: 1000; }
.name-box input { padding-left: 50px; }

.rule-list { display: grid; gap: 9px; }
.rule-row { min-height: 64px; padding: 10px 11px 10px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px; border: 1px solid var(--border); border-radius: 15px; background: rgba(255,255,255,.025); }
.rule-row b { display: block; margin-bottom: 3px; }
.rule-row small, .switch-row small, .menu-action small { color: var(--muted); font-size: 11px; }
.score-input { display: flex; align-items: center; gap: 5px; }
.score-input input { width: 58px; height: 40px; text-align: center; border: 0; border-radius: 11px; color: var(--text); background: var(--surface-2); font-weight: 1000; outline: none; }
.score-input em { color: var(--muted); font-style: normal; font-size: 12px; }
.foul-rule { box-shadow: inset 3px 0 0 var(--red); }
.normal-rule { box-shadow: inset 3px 0 0 var(--blue); }
.small-rule { box-shadow: inset 3px 0 0 var(--orange); }
.big-rule { box-shadow: inset 3px 0 0 var(--gold); }
.third-rule { box-shadow: inset 3px 0 0 #d979ff; }
.switch-row { margin: 14px 0 18px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch-row span { min-width: 0; }
.switch-row b { display: block; font-size: 13px; }
.switch-row input { display: none; }
.switch-row i { width: 44px; height: 25px; flex: 0 0 44px; position: relative; border-radius: 99px; background: #34463e; transition: .2s; }
.switch-row i::after { content: ""; width: 19px; height: 19px; position: absolute; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; }
.switch-row input:checked + i { background: var(--green); }
.switch-row input:checked + i::after { transform: translateX(19px); }

.primary-btn, .secondary-btn, .ghost-btn, .icon-btn { min-height: 43px; border-radius: 12px; padding: 10px 14px; font-weight: 900; }
.primary-btn { color: #06130d; background: linear-gradient(135deg, #35e58d, #1dbf70); box-shadow: 0 10px 24px rgba(29,191,112,.22); }
.primary-btn:active { transform: translateY(1px); }
.secondary-btn { color: var(--text); background: var(--surface-2); border: 1px solid var(--border); }
.ghost-btn { color: var(--text); background: transparent; border: 1px solid var(--border); }
.icon-btn { width: 43px; padding: 0; color: var(--text); background: var(--surface); border: 1px solid var(--border); }
.text-btn { color: var(--green); background: transparent; padding: 4px; font-size: 12px; font-weight: 900; }
.start-btn { width: 100%; min-height: 56px; display: flex; justify-content: center; align-items: center; gap: 12px; font-size: 15px; }
.start-btn span { font-size: 20px; }
.full-btn { width: 100%; margin-top: 9px; }

.match-top { min-height: 96px; padding: 18px 20px; display: grid; grid-template-columns: minmax(220px, 1fr) auto minmax(170px, 1fr); align-items: center; gap: 20px; margin-bottom: 18px; }
.match-title-wrap { display: flex; align-items: center; gap: 12px; min-width: 0; }
.match-title-wrap h2 { margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 21px; }
.live-dot { width: 10px; height: 10px; flex: 0 0 10px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 7px rgba(34,201,122,.1), 0 0 18px var(--green); animation: pulse 1.8s infinite; }
@keyframes pulse { 50% { opacity: .55; box-shadow: 0 0 0 11px rgba(34,201,122,0); } }
.match-metrics { display: flex; justify-content: center; }
.match-metrics > div { min-width: 116px; padding: 0 20px; text-align: center; border-left: 1px solid var(--border); }
.match-metrics > div:last-child { border-right: 1px solid var(--border); }
.match-metrics small { display: block; margin-bottom: 5px; color: var(--muted); font-size: 10px; }
.match-metrics strong { font-size: 16px; }
.match-actions { display: flex; justify-content: flex-end; gap: 8px; }
.game-layout { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(330px, .62fr); gap: 18px; align-items: start; }
.player-board { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.player-card { position: relative; min-width: 0; overflow: hidden; border-radius: 22px; background: linear-gradient(145deg, rgba(18,48,34,.97), rgba(10,27,19,.98)); border: 1px solid var(--border); box-shadow: var(--shadow); }
:root.light .player-card { background: linear-gradient(145deg, #fff, #edf6f1); }
.player-card.leader { border-color: rgba(240,201,92,.55); box-shadow: 0 20px 65px rgba(0,0,0,.28), inset 0 0 0 1px rgba(240,201,92,.16); }
.player-card::before { content: ""; position: absolute; width: 170px; height: 170px; right: -80px; top: -95px; border-radius: 50%; background: var(--player-color); opacity: .08; }
.player-main { padding: 18px 18px 14px; }
.player-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.player-ident { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar { width: 40px; height: 40px; flex: 0 0 40px; display: grid; place-items: center; border-radius: 13px; color: #07110d; background: var(--player-color); font-weight: 1000; box-shadow: inset 0 1px 0 rgba(255,255,255,.3); }
.player-name-input { width: 100%; min-width: 0; padding: 0; border: 0; outline: none; color: var(--text); background: transparent; font-size: 17px; font-weight: 1000; text-overflow: ellipsis; }
.player-seat { margin-top: 2px; color: var(--muted); font-size: 10px; }
.rank-badge { min-width: 35px; height: 29px; display: grid; place-items: center; border-radius: 10px; background: rgba(255,255,255,.06); color: var(--muted); font-size: 11px; font-weight: 1000; }
.leader .rank-badge { color: #171000; background: var(--gold); }
.score-zone { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; margin: 20px 0 16px; }
.score-value { font-size: clamp(48px, 6vw, 74px); line-height: .85; letter-spacing: -.07em; font-weight: 1000; }
.score-meta { text-align: right; }
.score-change { display: block; margin-bottom: 4px; color: var(--green); font-weight: 1000; }
.score-change.negative { color: var(--red); }
.score-meta small { color: var(--muted); }
.foul-meter { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.foul-meter span { color: var(--muted); font-size: 10px; }
.foul-dots { display: flex; gap: 5px; }
.foul-dots i { width: 8px; height: 8px; border-radius: 50%; background: rgba(255,255,255,.12); }
.foul-dots i.active { background: var(--red); box-shadow: 0 0 8px rgba(255,108,108,.55); }
.result-actions { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border-top: 1px solid var(--border); background: rgba(0,0,0,.12); }
.result-btn { min-height: 64px; padding: 8px 4px; border-right: 1px solid var(--border); color: var(--text); background: transparent; transition: .16s; }
.result-btn:last-child { border-right: 0; }
.result-btn:hover { background: rgba(255,255,255,.045); }
.result-btn b { display: block; margin-bottom: 3px; font-size: 13px; }
.result-btn small { color: var(--muted); font-size: 9px; }
.result-btn.foul b { color: var(--red); }
.result-btn.normal b { color: var(--blue); }
.result-btn.small-gold b { color: var(--orange); }
.result-btn.big-gold b { color: var(--gold); }
.action-guide { margin-top: 14px; padding: 16px 18px; display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.action-guide b { display: block; margin-bottom: 4px; }
.action-guide span { color: var(--muted); font-size: 11px; line-height: 1.55; }
.side-stack { display: grid; gap: 14px; position: sticky; top: 14px; }
.ranking-card, .history-card { padding: 18px; }
.card-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.saved-badge { padding: 5px 8px; border-radius: 8px; color: var(--green); background: rgba(34,201,122,.1); font-size: 9px; font-weight: 900; }
.ranking-list { display: grid; gap: 8px; }
.rank-row { min-height: 54px; padding: 8px 10px; display: grid; grid-template-columns: 30px 1fr auto; align-items: center; gap: 9px; border-radius: 13px; background: rgba(255,255,255,.035); }
.rank-number { color: var(--muted); font-size: 11px; font-weight: 1000; }
.rank-row:first-child .rank-number { color: var(--gold); }
.rank-player b { display: block; font-size: 13px; }
.rank-player small { color: var(--muted); font-size: 9px; }
.rank-score { text-align: right; font-size: 18px; font-weight: 1000; }
.history-list { max-height: 438px; overflow: auto; padding-right: 3px; }
.history-list::-webkit-scrollbar { width: 5px; }
.history-list::-webkit-scrollbar-thumb { border-radius: 10px; background: var(--surface-2); }
.history-item { padding: 11px 3px; display: grid; grid-template-columns: 30px 1fr auto; gap: 9px; align-items: start; border-bottom: 1px solid var(--border); }
.history-item:last-child { border-bottom: 0; }
.history-icon { width: 27px; height: 27px; display: grid; place-items: center; border-radius: 9px; background: var(--surface-2); font-size: 10px; font-weight: 1000; }
.history-body b { display: block; font-size: 11px; line-height: 1.4; }
.history-body small { color: var(--muted); font-size: 9px; }
.history-delta { color: var(--green); font-size: 11px; font-weight: 1000; }
.history-delta.negative { color: var(--red); }
.empty-state { padding: 25px 8px; color: var(--muted); text-align: center; font-size: 12px; }
.danger-text { color: var(--red); }

.modal { width: min(480px, calc(100% - 26px)); padding: 0; border: 0; color: var(--text); background: transparent; }
.modal::backdrop { background: rgba(0,0,0,.7); backdrop-filter: blur(7px); }
.modal-card { padding: 24px; border: 1px solid var(--border); border-radius: 24px; background: var(--surface-solid); box-shadow: 0 30px 100px rgba(0,0,0,.5); }
.modal-card > h3 { margin-bottom: 8px; text-align: center; }
.modal-card > p { color: var(--muted); text-align: center; line-height: 1.55; font-size: 12px; }
.modal-icon { width: 50px; height: 50px; margin: 0 auto 13px; display: grid; place-items: center; border-radius: 16px; font-size: 22px; font-weight: 1000; }
.foul-icon { color: var(--red); background: rgba(255,108,108,.12); }
.modal-preview { margin-top: 12px; padding: 12px; border-radius: 13px; background: var(--surface-2); color: var(--muted); font-size: 12px; text-align: center; }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 18px; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.modal-head h3 { margin: 0; }
.modal-head .icon-btn { min-height: 36px; width: 36px; background: var(--surface-2); }
.wide-modal { width: 100%; }
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.compact-switch { padding: 13px; border-radius: 13px; background: var(--surface-2); }
.action-modal { display: grid; gap: 8px; }
.menu-action { width: 100%; min-height: 64px; padding: 10px 12px; display: flex; align-items: center; gap: 12px; text-align: left; color: var(--text); background: var(--surface-2); border-radius: 14px; }
.menu-action > span { width: 34px; height: 34px; flex: 0 0 34px; display: grid; place-items: center; border-radius: 10px; color: var(--green); background: rgba(34,201,122,.1); font-weight: 1000; }
.menu-action b, .menu-action small { display: block; }
.danger-menu b, .danger-menu > span { color: var(--red); }

.toast { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 100; max-width: calc(100% - 30px); padding: 11px 16px; border: 1px solid rgba(255,255,255,.12); border-radius: 13px; color: #fff; background: rgba(6,19,13,.94); box-shadow: 0 16px 50px rgba(0,0,0,.4); opacity: 0; transform: translate(-50%, 18px); pointer-events: none; transition: .2s ease; text-align: center; font-size: 12px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1120px) {
  .game-layout { grid-template-columns: 1fr; }
  .side-stack { grid-template-columns: .7fr 1.3fr; position: static; }
  .history-list { max-height: 340px; }
  .match-top { grid-template-columns: 1fr auto; }
  .match-metrics { grid-column: 1 / -1; grid-row: 2; order: 3; border-top: 1px solid var(--border); padding-top: 14px; }
}

@media (max-width: 820px) {
  .app-shell { padding: 18px 14px 32px; }
  .hero-card { padding: 28px 24px; min-height: 170px; }
  .hero-balls { display: none; }
  .setup-layout { grid-template-columns: 1fr; }
  .name-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .player-board { grid-template-columns: 1fr; }
  .side-stack { grid-template-columns: 1fr; }
  .match-top { gap: 12px; }
  .match-metrics > div { min-width: 0; flex: 1; padding: 0 10px; }
}

@media (max-width: 560px) {
  .app-shell { padding: 13px 10px calc(24px + env(safe-area-inset-bottom)); }
  .app-header { margin-bottom: 16px; }
  .ball-logo { width: 46px; height: 46px; flex-basis: 46px; }
  .brand-subtitle, .header-actions .ghost-btn { display: none; }
  h1 { font-size: 23px; }
  .hero-card { padding: 24px 20px; border-radius: 21px; }
  .hero-card h2 { font-size: 31px; }
  .hero-card p { font-size: 13px; }
  .setup-main, .setup-side { padding: 18px 15px; }
  .form-grid, .name-grid, .settings-grid { grid-template-columns: 1fr; }
  .field-wide { grid-column: auto; }
  .match-top { padding: 15px; grid-template-columns: minmax(0,1fr) auto; }
  .match-actions #rulesBtn { display: none; }
  .match-metrics { overflow: hidden; }
  .match-metrics > div { padding: 0 5px; }
  .match-metrics strong { font-size: 13px; }
  .player-main { padding: 15px 14px 12px; }
  .result-btn { min-height: 62px; }
  .result-btn b { font-size: 12px; }
  .action-guide { align-items: stretch; flex-direction: column; }
  .action-guide button { width: 100%; }
  .ranking-card, .history-card { padding: 16px 14px; }
  .modal-card { padding: 20px 16px; }
}
