:root {
  color-scheme: dark;
  --bg: #09051a;
  --card: #151033;
  --card-2: #22184d;
  --text: #f5f6ff;
  --muted: #b8b5d7;
  --line: #40357b;
  --primary: #3b82f6;
  --danger: #ef4444;
  --warning: #f7b500;
  --sticky-actions-offset: 0px;
  --radius: 14px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: #0A071C;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100%;
}

body {
  padding: calc(12px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
}

.app-shell {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  display: grid;
  gap: 14px;
}

.app-header {
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 20;
  background: #0A071C;
  padding: 0px 0 0px;
  transition: padding 160ms ease;
}

.app-logo {
  width: min(100%, 300px);
  height: auto;
  margin: 0 auto 1px;
  display: block;
  filter: drop-shadow(0 10px 24px rgba(0, 0, 0, 0.35));
  transform-origin: top center;
  transition: width 160ms ease, margin 160ms ease;
}

.app-header.compact {
  padding: 0;
}

.app-header.compact .app-logo {
  width: min(100%, 132px);
  margin-bottom: 0;
}

.app-header h1 {
  margin: 0 0 2px;
  font-size: 1.8rem;
  letter-spacing: 0.2px;
  color: #ffffff;
}

.app-header .muted {
  color: #4f5870;
}

.muted {
  color: var(--muted);
  margin: 6px 0 0;
}

.card {
  width: 100%;
  background: linear-gradient(160deg, rgba(26, 19, 59, 0.93), rgba(13, 10, 34, 0.96));
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.4);
}

.hidden {
  display: none;
}

.field {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.setup-top-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

select,
input[type="text"],
input[type="number"] {
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  padding: 12px;
  font-size: 16px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="text"]:focus,
select:focus {
  outline: none;
  border-color: #7fb0ff;
  box-shadow: 0 0 0 2px rgba(91, 156, 255, 0.25);
}

input:disabled {
  opacity: 0.75;
}

.name-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.team-group {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  background: #1a1540;
}

.team-title {
  margin: 0 0 10px;
  font-weight: 700;
}

.team-partners {
  display: grid;
  grid-template-columns: repeat(2, minmax(1, 1fr));
  gap: 8px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 0.98rem;
  font-weight: 600;
  background: #3b3270;
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.3);
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  background: var(--primary);
  color: #ffffff;
}

.btn.danger {
  background: var(--danger);
  color: #ffffff;
}

.btn.warning {
  background: var(--warning);
  color: #2b2101;
}

.btn.export {
  background: #4c5a86;
  color: #eef3ff;
}

.mini-btn {
  padding: 6px 10px;
  font-size: 0.82rem;
  border-radius: 8px;
  min-height: 30px;
  background: #5e53a8;
  color: #f4f0ff;
}

.quick-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.quick-row {
  display: grid;
  gap: 6px;
}

.quick-row-single {
  grid-template-columns: 1fr;
}

.quick-row-double {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.quick-btn {
  width: 100%;
  min-width: 0;
  padding: 6px 4px;
  min-height: 28px;
  border-radius: 8px;
  font-size: 0.78rem;
  background: #3f3870;
  color: #f1eeff;
  box-shadow: none;
}

.quick-btn.quick-btn-zero {
  background: #8e6f14;
  color: #fff3c9;
}

.quick-btn.quick-btn-one {
  background: #2d6ed9;
  color: #eaf2ff;
}

.quick-btn.quick-btn-two {
  background: #22854c;
  color: #e9fff0;
}

.quick-btn.quick-btn-plus {
  background: #872938;
  color: #ffe5e5;
}

.quick-btn.quick-btn-minus {
  background: #1f6a36;
  color: #dcffe6;
}

.quick-btn.quick-btn-mul {
  background: #5d111a;
  color: #ffe7eb;
}

.quick-btn.active {
  filter: none;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.quick-btn.quick-btn-zero.active {
  background: #ffd765;
  color: #2b1f00;
}

.quick-btn.quick-btn-one.active {
  background: #72a4ff;
  color: #0d2a5e;
}

.quick-btn.quick-btn-two.active {
  background: #8ee8a4;
  color: #0b3a1d;
}

.quick-btn.quick-btn-plus.active {
  background: #ff8a8a;
  color: #2a0d0d;
}

.quick-btn.quick-btn-minus.active {
  background: #8ee8a4;
  color: #08210f;
}

.quick-btn.quick-btn-mul.active {
  background: #b12b3d;
  color: #ffeaf0;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.score-meta {
  width: 100%;
  text-align: center;
}

.score-meta h2 {
  margin: 0;
}

.score-meta .muted {
  margin-top: 6px;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-top: 10px;
}

table {
  width: 100%;
  min-width: 100%;
  border-collapse: collapse;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: center;
  vertical-align: top;
}

th.hand-col,
td.hand-col {
  width: 56px;
  min-width: 56px;
  max-width: 56px;
  padding-left: 6px;
  padding-right: 6px;
}

thead th {
  background: linear-gradient(180deg, #251f4e, #1e1a3e);
  font-weight: 700;
}

tfoot td {
  background: linear-gradient(180deg, #2a2160, #1a1546);
  color: #f8f9ff;
  font-weight: 800;
  font-size: 1.16rem;
  border-top: 2px solid #7f72ee;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

tfoot {
  position: static;
  bottom: var(--sticky-actions-offset);
  z-index: 6;
}

tfoot td {
  box-shadow: 0 -10px 18px rgba(9, 6, 36, 0.65);
}

tfoot td:first-child {
  color: #ffe28f;
}

.score-compare-panel {
  margin-top: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}

.compare-row {
  display: grid;
  grid-template-columns: 86px repeat(var(--compare-count, 2), minmax(0, 1fr));
}

.compare-label,
.compare-value {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
}

.compare-label {
  background: #ffffff;
  color: #101b43;
  border-right: 1px solid rgba(80, 55, 0, 0.25);
  font-size: 1.05rem;
}

.compare-value {
  font-size: 1.3rem;
  color: #19213a;
}

.compare-value.neutral {
  background: linear-gradient(180deg, #3a2f86, #251c63);
  color: #f5f7ff;
}

.compare-value.rank-1 {
  background: linear-gradient(180deg, #adf7be, #86e6a5);
  color: #10361d;
}

.compare-value.rank-2 {
  background: linear-gradient(180deg, #d2f5b7, #bdeca1);
  color: #2f4a10;
}

.compare-value.rank-3 {
  background: linear-gradient(180deg, #ffdcb3, #ffc79a);
  color: #5a2f11;
}

.compare-value.rank-4 {
  background: linear-gradient(180deg, #ffb7b7, #ff8f8f);
  color: #4a1414;
}

.compare-value.low {
  background: linear-gradient(180deg, #adf7be, #86e6a5);
  color: #10361d;
}

.compare-value.high {
  background: linear-gradient(180deg, #ffb7b7, #ff8f8f);
  color: #4a1414;
}

.compare-value.draw,
.compare-value.diff {
  background: linear-gradient(180deg, #ffe58c, #ffd766);
  color: #3b2a00;
}

.compare-value.leader {
  background: linear-gradient(180deg, #fff3bf, #ffe58c);
  color: #3b2a00;
  font-size: 1.05rem;
}

.compare-row + .compare-row {
  border-top: 1px solid rgba(80, 55, 0, 0.25);
}

input[disabled] {
  opacity: 0.55;
}

.cell-stack {
  display: grid;
  gap: 8px;
}

.partner-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(64px, 1fr));
  gap: 8px;
}

.partner-cell {
  display: grid;
  gap: 6px;
}

.partner-label {
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
}

.sign-btn {
  width: 100%;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 10px;
  font-size: 0.95rem;
  background: #372f69;
  color: #eef3ff;
}

.team-hand-total {
  display: block;
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
}

.table-wrap input[type="text"],
.table-wrap input[type="number"] {
  text-align: center;
}

.editing-row {
  background: rgba(96, 82, 184, 0.16);
}

.editing-badge {
  color: #9fe8be !important;
  font-size: 0.75rem;
}

.hand-meta {
  justify-items: center;
}

.icon-btn {
  border: 1px solid var(--line);
  background: #3f357f;
  color: #efeaff;
  border-radius: 9px;
  min-width: 34px;
  min-height: 30px;
  cursor: pointer;
  font-size: 0.9rem;
}

.icon-btn.confirm {
  background: #1f7f68;
  border-color: #3ac8a2;
  color: #e5fff8;
}

.icon-btn:active {
  transform: translateY(1px);
}

.cell-stack small {
  color: var(--muted);
}

.special-select {
  min-width: 150px;
}

.status-select {
  min-width: 140px;
}

.radio-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  color: var(--muted);
  font-size: 0.88rem;
}

.sticky-actions {
  position: relative;
  bottom: 0;
  z-index: 7;
  background: linear-gradient(180deg, rgba(19, 14, 45, 0.5), rgba(19, 14, 45, 1));
  padding-top: 10px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.sticky-actions .btn {
  width: 100%;
  flex: 0 0 auto;
  margin: 0;
}

.modal {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, #19143a, #120f2a);
  color: var(--text);
  max-width: 520px;
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.modal-content {
  padding: 10px;
}

#appDialogMessage {
  margin-top: 8px;
  line-height: 1.4;
  white-space: pre-line;
}

.rules-list {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.rule-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #241d4d;
  padding: 10px;
}

.rule-item h4 {
  margin: 0 0 6px;
  font-size: 0.98rem;
}

.rule-item p {
  margin: 0;
  color: var(--muted);
  line-height: 1.35;
}

.export-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #1a1540;
  padding: 12px;
  margin-top: 10px;
  display: grid;
  gap: 6px;
}

.export-logo {
  width: min(100%, 300px);
  display: block;
  margin: 4px auto 8px;
}

.export-card h4 {
  margin: 0 0 4px;
}

.export-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.export-row strong {
  color: #f1f4ff;
}

.winner-badge {
  color: #ffd166;
  font-size: 0.84rem;
  margin-left: 6px;
  white-space: nowrap;
}

@media (max-width: 520px) {
  body {
    padding: calc(8px + env(safe-area-inset-top)) 8px calc(10px + env(safe-area-inset-bottom));
  }

  .btn {
    flex: 1 1 100%;
  }

  .sticky-actions .btn {
    flex: 0 0 auto;
  }

  #scoreScreen .toolbar .btn {
    flex: 1 1 calc(50% - 5px);
    min-width: 0;
  }

  .card {
    padding: 10px;
  }

  th,
  td {
    padding: 8px 6px;
    font-size: 0.92rem;
  }

  th.hand-col,
  td.hand-col {
    width: 46px;
    min-width: 46px;
    max-width: 46px;
    padding-left: 4px;
    padding-right: 4px;
  }

  input[type="number"],
  input[type="text"] {
    padding: 10px;
    font-size: 16px;
  }

  .mini-btn {
    width: 100%;
  }

  .name-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .name-grid .field span {
    font-size: 0.9rem;
  }

  .name-grid .field input {
    padding: 10px 8px;
    font-size: 0.92rem;
  }

  .team-group {
    padding: 8px;
  }
}
