/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, 'Helvetica Neue', sans-serif;
  background: #0a0a0a;
  color: #f0f0f0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* === Views === */
.view { display: none; height: 100svh; width: 100vw; position: absolute; top: 0; left: 0; }
.view.active { display: flex; }

/* === Home === */
.home-container {
  margin: auto;
  text-align: center;
  padding: 20px;
}
.home-title {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 8px;
  color: #fff;
  margin-bottom: 8px;
}
.home-subtitle {
  font-size: 1rem;
  color: #888;
  margin-bottom: 40px;
}
.home-actions { display: flex; flex-direction: column; gap: 12px; max-width: 280px; margin: 0 auto; }

/* === Buttons === */
.btn {
  border: none; border-radius: 8px; padding: 14px 24px; font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: opacity .15s;
}
.btn:active { opacity: .7; }
.btn-primary { background: #e8b100; color: #000; }
.btn-secondary { background: #222; color: #ccc; }
.btn-small { padding: 8px 14px; font-size: .85rem; background: #333; color: #ccc; border: none; border-radius: 6px; cursor: pointer; }
.btn-danger { background: #c0392b !important; color: #fff !important; }

/* === Forms === */
.form-container {
  margin: auto; padding: 24px; max-width: 420px; width: 100%;
}
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: .85rem; color: #999; margin-bottom: 6px; }
.form-group input[type="text"],
.form-group input[type="number"] {
  width: 100%; padding: 10px 12px; border-radius: 6px; border: 1px solid #333;
  background: #181818; color: #f0f0f0; font-size: 1rem;
}
.form-actions { display: flex; gap: 10px; margin-top: 20px; }
.form-actions .btn { flex: 1; }
.error-text { color: #e74c3c; font-size: .85rem; margin-top: 6px; }

/* Preset buttons */
.preset-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-preset {
  padding: 8px 16px; border-radius: 6px; border: 1px solid #333;
  background: #181818; color: #aaa; font-size: .85rem; cursor: pointer; transition: all .15s;
}
.btn-preset.active { background: #e8b100; color: #000; border-color: #e8b100; }
.custom-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.checkbox-label { display: flex; align-items: center; gap: 6px; font-size: .85rem; color: #999; flex-direction: row !important; }
.checkbox-label input { width: auto; }

/* === Share === */
.share-container { text-align: center; }
.share-code { font-size: 2.5rem; font-weight: 800; letter-spacing: 10px; color: #e8b100; margin: 16px 0; }
.share-qr { margin: 16px auto; width: 180px; height: 180px; background: #fff; border-radius: 12px; display: flex; align-items: center; justify-content: center; padding: 10px; }
.share-qr svg { width: 100%; height: 100%; }
.share-link-row { display: flex; gap: 8px; margin-top: 12px; }
.share-link-row input { flex: 1; padding: 8px; border-radius: 6px; border: 1px solid #333; background: #181818; color: #ccc; font-size: .8rem; }

/* === Scoreboard === */
.board-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 16px; background: #111; border-bottom: 1px solid #222;
  min-height: 44px; z-index: 10;
}
.board-info { display: flex; align-items: center; gap: 8px; font-size: .85rem; overflow: hidden; }
#board-room-name { color: #888; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 120px; }
.rule-badge, .set-badge { background: #222; padding: 2px 8px; border-radius: 4px; font-size: .75rem; color: #e8b100; white-space: nowrap; }
.board-actions-top { display: flex; gap: 6px; }
.btn-icon {
  width: 36px; height: 36px; border-radius: 8px; border: 1px solid #333;
  background: #181818; color: #ccc; font-size: 1.1rem; cursor: pointer; display: flex; align-items: center; justify-content: center;
}

.board-body {
  flex: 1; display: flex; position: relative; overflow: hidden;
  /* 점수 자릿수 크기: 패널 영역을 최대한 채움 */
  --digit-w: min(20vw, 38vh);
  --digit-h: calc(var(--digit-w) * 1.5);
  --score-size: calc(var(--digit-w) * 1.25);
}
.board-body.swapped { flex-direction: row-reverse; }

/* Team panel */
.team-panel {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 10px 2px; cursor: pointer; position: relative; transition: background .1s;
}
.team-panel:active { background: rgba(255,255,255,.03); }
.team-name {
  font-size: calc(var(--score-size) * 0.25); font-weight: 600; color: #ccc; margin-bottom: calc(var(--digit-w) * 0.04);
  max-width: 90%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  line-height: 1.2;
}
.team-sets {
  font-size: calc(var(--score-size) * 0.3); color: #e8b100; font-weight: 700; margin-bottom: calc(var(--digit-w) * 0.08);
  line-height: 1.2;
}
.team-sets::before { content: 'SET '; font-size: calc(var(--score-size) * 0.3); color: #888; font-weight: 400; }

/* Divider */
.board-divider {
  width: 2px; background: #222; display: flex; align-items: center; justify-content: center; position: relative;
}
.vs-label {
  position: absolute; background: #181818; padding: 4px 6px; font-size: .7rem; color: #555; border-radius: 4px;
}

/* === FLIP DIGITS === */
.score-area { display: flex; gap: clamp(4px, 1vw, 12px); }
.flip-container {
  width: var(--digit-w); height: var(--digit-h); perspective: 300px; position: relative;
}

.flip-card {
  width: 100%; height: 100%; position: relative;
}

/* 각 자릿수 상/하 패널 */
.flip-front, .flip-back {
  position: absolute; width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  font-size: var(--score-size); font-weight: 800; color: #f0f0f0;
  background: #1a1a1a; border-radius: 8px;
  backface-visibility: hidden;
}

/* 플립 상반 */
.flip-top, .flip-bottom {
  position: absolute; width: 100%; height: 50%; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
.flip-top {
  top: 0; border-radius: 8px 8px 0 0;
  background: #1a1a1a;
  border-bottom: 1px solid #111;
}
.flip-bottom {
  bottom: 0; border-radius: 0 0 8px 8px;
  background: #1a1a1a;
}

.digit-box {
  position: relative; width: 100%; height: 100%;
}

/* 위쪽 반 */
.digit-box .top {
  position: absolute; top: 0; left: 0; width: 100%; height: 50%; overflow: hidden;
  background: #1a1a1a; border-radius: 8px 8px 0 0;
  border-bottom: 1.5px solid #111;
  display: flex; align-items: flex-end; justify-content: center;
}
.digit-box .top span {
  transform: translateY(50%);
  font-size: inherit; font-weight: 800; color: #f0f0f0; line-height: 1;
}

/* 아래쪽 반 */
.digit-box .bottom {
  position: absolute; bottom: 0; left: 0; width: 100%; height: 50%; overflow: hidden;
  background: #1e1e1e; border-radius: 0 0 8px 8px;
  display: flex; align-items: flex-start; justify-content: center;
}
.digit-box .bottom span {
  transform: translateY(-50%);
  font-size: inherit; font-weight: 800; color: #f0f0f0; line-height: 1;
}

/* 플립 애니메이션 - 위에서 아래로 넘기기 */
.digit-box .flip-panel-top {
  position: absolute; top: 0; left: 0; width: 100%; height: 50%; overflow: hidden;
  background: #1a1a1a; border-radius: 8px 8px 0 0;
  border-bottom: 1.5px solid #111;
  display: flex; align-items: flex-end; justify-content: center;
  transform-origin: bottom center;
  z-index: 3;
}
.digit-box .flip-panel-top span {
  transform: translateY(50%);
  font-size: inherit; font-weight: 800; color: #f0f0f0; line-height: 1;
}

.digit-box .flip-panel-bottom {
  position: absolute; bottom: 0; left: 0; width: 100%; height: 50%; overflow: hidden;
  background: #1e1e1e; border-radius: 0 0 8px 8px;
  display: flex; align-items: flex-start; justify-content: center;
  transform-origin: top center;
  transform: rotateX(90deg);
  z-index: 2;
}
.digit-box .flip-panel-bottom span {
  transform: translateY(-50%);
  font-size: inherit; font-weight: 800; color: #f0f0f0; line-height: 1;
}

/* 애니메이션 */
.digit-box.flipping .flip-panel-top {
  animation: flipDown1 0.3s ease-in forwards;
}
.digit-box.flipping .flip-panel-bottom {
  animation: flipDown2 0.3s 0.3s ease-out forwards;
}

@keyframes flipDown1 {
  0% { transform: rotateX(0deg); }
  100% { transform: rotateX(-90deg); }
}
@keyframes flipDown2 {
  0% { transform: rotateX(90deg); }
  100% { transform: rotateX(0deg); }
}

/* 그림자 효과 */
.digit-box .top::after {
  content: ''; position: absolute; bottom: 0; left: 0; width: 100%; height: 4px;
  background: linear-gradient(to top, rgba(0,0,0,.3), transparent);
}
.digit-box .bottom::after {
  content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 4px;
  background: linear-gradient(to bottom, rgba(0,0,0,.3), transparent);
}

/* === Board controls === */
.board-controls {
  display: flex; justify-content: center; align-items: center; gap: 12px;
  padding: 10px 16px; background: #111; border-top: 1px solid #222;
}
.btn-ctrl {
  padding: 8px 20px; border-radius: 8px; border: 1px solid #333;
  background: #181818; color: #ccc; font-size: .95rem; cursor: pointer;
  transition: background .15s;
}
.btn-ctrl:active { background: #333; }
.btn-ctrl:disabled { opacity: .3; cursor: default; }

/* === Board overlay (세트/경기 종료) === */
.board-overlay {
  position: absolute; inset: 0; background: rgba(0,0,0,.85);
  display: flex; align-items: center; justify-content: center; z-index: 20;
}
.overlay-content { text-align: center; padding: 20px; }
.overlay-content h2 { font-size: 1.8rem; color: #e8b100; margin-bottom: 12px; }
.overlay-content p { color: #ccc; margin-bottom: 16px; }

/* === Side menu === */
.side-menu { display: none; position: absolute; inset: 0; z-index: 50; }
.side-menu.open { display: flex; }
.menu-backdrop { flex: 1; background: rgba(0,0,0,.5); }
.menu-panel {
  width: 260px; background: #141414; padding: 20px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 6px;
}
.menu-panel h3 { color: #e8b100; margin-bottom: 8px; font-size: 1rem; }
.menu-panel button {
  width: 100%; padding: 10px 12px; border: none; border-radius: 6px;
  background: #222; color: #ccc; font-size: .9rem; cursor: pointer; text-align: left;
}
.menu-panel button:active { background: #333; }
.menu-panel hr { border: none; border-top: 1px solid #222; margin: 4px 0; }
.menu-section { display: flex; flex-direction: column; gap: 6px; }

/* === Modal === */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.7); z-index: 100;
  display: flex; align-items: center; justify-content: center;
}
.modal-content {
  background: #181818; border-radius: 12px; padding: 24px; max-width: 380px; width: 90%;
}
.modal-content h3 { color: #e8b100; margin-bottom: 16px; }

/* === view-board flex layout === */
#view-board { flex-direction: column; }

/* === Closed view === */
#view-closed { align-items: center; justify-content: center; }
#view-closed h1 { font-size: 1.5rem; color: #e8b100; margin-bottom: 12px; }
#view-closed p { color: #888; margin-bottom: 24px; }

/* === Mobile portrait adjustments === */
@media (max-width: 480px) and (orientation: portrait) {
  .board-body {
    --digit-w: 20vw;
    --digit-h: calc(var(--digit-w) * 1.5);
    --score-size: calc(var(--digit-w) * 0.85);
  }
  .board-controls { padding: 8px; gap: 8px; }
  .btn-ctrl { padding: 6px 14px; font-size: .85rem; }
}

/* === Landscape optimizations === */
@media (orientation: landscape) and (max-height: 500px) {
  .board-body {
    --digit-w: min(16vw, 32vh);
    --digit-h: calc(var(--digit-w) * 1.5);
    --score-size: calc(var(--digit-w) * 0.85);
  }
  .board-header { padding: 4px 12px; min-height: 32px; }
  .board-controls { padding: 4px 12px; }
  .btn-ctrl { padding: 4px 14px; }
}
