/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --green-dark: #2d8a3e;
  --green-main: #4caf50;
  --green-light: #a5d6a7;
  --green-pale: #e8f5e9;
  --tangerine: #ff8c00;
  --tangerine-dark: #e65100;
  --tangerine-light: #ffb74d;
  --white: #ffffff;
  --text-dark: #1b5e20;
  --select-bg: rgba(33, 150, 243, 0.25);
  --select-border: rgba(33, 150, 243, 0.6);
  --board-cols: 17;
  --board-rows: 10;
  --cell-size: 42px;
  --board-gap: 2px;
  --board-padding: 8px;
  --side-panel-width: 120px;
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: 'Apple SD Gothic Neo', 'Malgun Gothic', 'Noto Sans KR', sans-serif;
  background: var(--green-dark);
  color: var(--text-dark);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  width: 100%;
  height: 100%;
  height: 100dvh;
}

/* ===== Screens ===== */
.screen {
  display: none;
  width: 100%;
  height: 100%;
  height: 100dvh;
}

.screen.active {
  display: flex;
}

#start-screen.active {
  align-items: center;
  justify-content: center;
}

/* ===== Start Screen ===== */
#start-screen {
  background: repeating-linear-gradient(
    45deg,
    var(--green-pale),
    var(--green-pale) 12px,
    var(--white) 12px,
    var(--white) 24px
  );
  border: 8px solid var(--green-main);
}

.start-content {
  text-align: center;
  padding: 2rem;
}

.title {
  font-size: clamp(2.5rem, 8vw, 4rem);
  font-weight: 900;
  background: linear-gradient(135deg, var(--green-main), var(--tangerine));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: none;
  filter: drop-shadow(2px 2px 0 var(--white));
  margin-bottom: 0.5rem;
}

.subtitle {
  font-size: 1.1rem;
  color: var(--green-dark);
  margin-bottom: 2rem;
}

.btn-play {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s;
}

.btn-play:hover {
  transform: scale(1.05);
}

.btn-play:active {
  transform: scale(0.95);
}

.tangerine-icon.large {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--tangerine-light), var(--tangerine) 60%, var(--tangerine-dark));
  color: var(--white);
  font-size: 1.8rem;
  font-weight: 800;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
  position: relative;
}

.tangerine-icon.large::before {
  content: '';
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 12px;
  background: var(--green-main);
  border-radius: 50% 50% 0 0;
}

/* ===== Game Screen ===== */
#game-screen {
  position: relative;
  flex-direction: column;
  background: var(--green-dark);
  padding: 0;
  min-height: 0;
}

#game-screen.active {
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.game-layout {
  display: flex;
  flex: 1;
  width: 100%;
  min-height: 0;
  padding: 8px;
  gap: 8px;
  overflow: hidden;
}

.mobile-header {
  display: none;
  width: 100%;
  background: var(--green-main);
  color: var(--white);
  padding: 10px 16px;
  justify-content: space-around;
  align-items: center;
  flex-shrink: 0;
}

.mobile-stat {
  text-align: center;
}

.mobile-stat .label {
  display: block;
  font-size: 0.7rem;
  opacity: 0.85;
}

.mobile-stat span:last-child {
  font-size: 1.3rem;
  font-weight: 800;
}

/* ===== Board Area ===== */
.board-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.level-hint {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  padding: 8px 14px;
  margin-bottom: 8px;
  font-size: clamp(0.75rem, 1.5vw, 0.95rem);
  font-weight: 600;
  color: var(--green-dark);
  text-align: center;
  flex-shrink: 0;
}

.board-wrapper {
  position: relative;
  flex: 1;
  min-height: 0;
  border: 6px solid var(--green-main);
  border-radius: 8px;
  background: repeating-linear-gradient(
    0deg,
    var(--green-pale) 0px,
    var(--green-pale) 20px,
    var(--white) 20px,
    var(--white) 40px
  ),
  repeating-linear-gradient(
    90deg,
    var(--green-pale) 0px,
    var(--green-pale) 20px,
    var(--white) 20px,
    var(--white) 40px
  );
  overflow: hidden;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--board-cols), var(--cell-size));
  grid-template-rows: repeat(var(--board-rows), var(--cell-size));
  gap: var(--board-gap);
  padding: 4px;
  width: fit-content;
  height: fit-content;
  touch-action: none;
}

/* ===== Tangerine Cell ===== */
.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
}

.cell.empty {
  pointer-events: none;
}

.tangerine {
  width: calc(var(--cell-size) - 6px);
  height: calc(var(--cell-size) - 6px);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--tangerine-light), var(--tangerine) 55%, var(--tangerine-dark));
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--white);
  font-weight: 800;
  font-size: calc(var(--cell-size) * 0.38);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  position: relative;
  transition: transform 0.15s, opacity 0.3s;
}

.tangerine::before {
  content: '';
  position: absolute;
  top: -3px;
  left: 50%;
  transform: translateX(-50%);
  width: 10px;
  height: 6px;
  background: var(--green-main);
  border-radius: 50% 50% 0 0;
}

.cell.selected .tangerine {
  box-shadow: 0 0 0 3px var(--select-border), 0 2px 8px rgba(0, 0, 0, 0.3);
  filter: brightness(1.1);
}

.cell.popping .tangerine {
  animation: pop 0.35s ease forwards;
}

@keyframes pop {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.3); opacity: 0.7; }
  100% { transform: scale(0); opacity: 0; }
}

/* ===== Selection Box ===== */
.selection-box {
  position: absolute;
  border: 2px solid var(--select-border);
  background: var(--select-bg);
  pointer-events: none;
  border-radius: 4px;
  z-index: 10;
}

.selection-box.hidden {
  display: none;
}

/* ===== Side Panel (Desktop) ===== */
.side-panel {
  width: var(--side-panel-width);
  flex-shrink: 0;
  align-self: stretch;
  background: var(--green-main);
  border-radius: 8px;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: var(--white);
}

.panel-stat {
  text-align: center;
  width: 100%;
}

.panel-label {
  display: block;
  font-size: 0.75rem;
  opacity: 0.85;
  margin-bottom: 2px;
}

.panel-value {
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1;
}

.panel-value.op {
  font-size: 2.2rem;
}

.panel-target {
  font-size: 0.8rem;
  text-align: center;
  line-height: 1.4;
  opacity: 0.95;
  min-height: 2.5em;
}

.timer-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  margin-top: 8px;
}

.timer-bar-container {
  width: 24px;
  flex: 1;
  min-height: 200px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  overflow: hidden;
  margin: 8px 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.timer-bar {
  width: 100%;
  background: linear-gradient(to top, #ffeb3b, #ffc107);
  border-radius: 0 0 12px 12px;
  transition: height 0.3s linear;
  height: 100%;
}

.timer-text {
  font-size: 1.2rem;
  font-weight: 700;
}

.panel-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.bgm-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  cursor: pointer;
}

.bgm-toggle input {
  accent-color: var(--tangerine);
}

.btn-reset {
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid var(--white);
  color: var(--white);
  padding: 6px 16px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-reset:hover {
  background: rgba(255, 255, 255, 0.35);
}

/* ===== Mobile Footer ===== */
.mobile-footer {
  display: none;
  width: 100%;
  background: var(--green-main);
  padding: 10px 16px;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

/* ===== Result Overlay ===== */
.result-overlay {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.8);
  align-items: center;
  justify-content: center;
}

.result-overlay.active {
  display: flex;
}

.result-content {
  text-align: center;
  padding: 2rem;
}

.result-tangerine {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--tangerine-light), var(--tangerine) 60%, var(--tangerine-dark));
  color: var(--white);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  margin-bottom: 1.5rem;
  position: relative;
}

.result-tangerine::before {
  content: '';
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 14px;
  background: var(--green-main);
  border-radius: 50% 50% 0 0;
}

.result-label {
  font-size: 1rem;
  opacity: 0.9;
}

.result-value {
  font-size: 3rem;
  font-weight: 900;
  line-height: 1.1;
}

.result-message {
  color: var(--white);
  font-size: 1.1rem;
  margin-bottom: 1.5rem;
}

.result-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

.btn-result {
  padding: 12px 32px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  border: none;
  min-width: 220px;
  transition: transform 0.15s, background 0.15s;
  background: var(--green-main);
  color: var(--white);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.btn-result:hover {
  background: var(--green-dark);
  transform: scale(1.03);
}

.btn-result:active {
  transform: scale(0.98);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .mobile-header {
    display: flex;
  }

  .mobile-footer {
    display: flex;
  }

  .side-panel {
    display: none;
  }

  .level-hint {
    font-size: 0.75rem;
    padding: 5px 8px;
    margin-bottom: 4px;
  }

  .game-layout {
    flex: 1;
    padding: 4px;
    gap: 0;
    min-height: 0;
  }

  .board-area {
    flex: 1;
    min-height: 0;
  }

  .board-wrapper {
    border-width: 4px;
    flex: 1;
  }

  #game-screen.active {
    height: 100dvh;
  }

  .result-tangerine {
    width: 150px;
    height: 150px;
  }

  .result-value {
    font-size: 2.5rem;
  }

  .btn-result {
    min-width: 180px;
    padding: 14px 24px;
  }
}

@media (min-width: 769px) {
  .game-layout {
    padding: 10px;
    gap: 10px;
  }
}
