:root {
  --felt: #1b3d2f;
  --felt-deep: #12281f;
  --felt-light: #2a5844;
  --wood: #d4a04a;
  --panel: rgba(18, 40, 30, 0.82);
  --panel-border: rgba(212, 180, 110, 0.28);
  --text: #f4efe4;
  --text-muted: #a8bfb2;
  --cho: #2f8f68;
  --han: #c43a3a;
  --accent: #c9a227;
  --accent-text: #1a2a20;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  min-height: 100%;
  min-height: 100dvh;
  color: #f4efe4;
  font-family: "Noto Sans KR", "Apple SD Gothic Neo", sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(80, 140, 100, 0.35), transparent 55%),
    radial-gradient(ellipse 60% 40% at 80% 100%, rgba(40, 80, 50, 0.4), transparent 50%),
    linear-gradient(160deg, var(--felt-light), var(--felt) 40%, var(--felt-deep));
  background-attachment: fixed;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 1100px;
  margin: 0 auto;
  padding: 12px 12px 20px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 2px 14px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--panel-border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(145deg, #f6ead0, #e0c07a);
  color: #8b1e1e;
  font-family: "Nanum Myeongjo", "Noto Serif KR", serif;
  font-weight: 700;
  font-size: 1.25rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

header h1 {
  font-family: "Noto Serif KR", "Nanum Myeongjo", serif;
  font-size: clamp(1.15rem, 4vw, 1.55rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: #f7efd8;
}

#status-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

#turn-indicator {
  font-size: 0.88rem;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--panel-border);
  white-space: nowrap;
}

#turn-indicator[data-side="cho"] { color: #7ddeb8; }
#turn-indicator[data-side="han"] { color: #ff8f8f; }

#check-banner {
  font-family: "Noto Serif KR", serif;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 7px 12px;
  border-radius: 8px;
  background: linear-gradient(135deg, #c62828, #8e0000);
  color: #fff;
  animation: check-pulse 0.9s ease-in-out infinite;
  box-shadow: 0 0 18px rgba(200, 40, 40, 0.45);
}

#check-banner.hidden { display: none; }

@keyframes check-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

#install-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 0.85rem;
  color: var(--text-muted);
  backdrop-filter: blur(8px);
}

#install-banner.hidden { display: none; }
#install-banner span { flex: 1; }
#install-banner .btn {
  width: auto;
  margin: 0;
  padding: 7px 12px;
  white-space: nowrap;
}

#main-layout {
  display: flex;
  gap: 18px;
  flex: 1;
  min-height: 0;
}

#game-area {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  flex: 1;
  min-width: 0;
}

#board-container {
  position: relative;
  width: min(100%, 540px);
  aspect-ratio: 540 / 600;
  border-radius: 14px;
  overflow: hidden;
  box-shadow:
    0 2px 0 rgba(255, 230, 170, 0.24) inset,
    0 22px 58px rgba(0, 0, 0, 0.5),
    0 0 0 1px rgba(240, 210, 140, 0.28),
    0 0 28px rgba(160, 110, 35, 0.12);
  touch-action: none;
  animation: board-in 0.55s ease-out both;
}

@keyframes board-in {
  from { opacity: 0; transform: translateY(12px) scale(0.985); }
  to { opacity: 1; transform: none; }
}

#board-container canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#board-container canvas:last-child {
  cursor: pointer;
  z-index: 2;
}

aside#side-panel {
  width: 230px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-section {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 14px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}

.panel-section h2 {
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  color: #cbb98a;
  margin-bottom: 10px;
  font-weight: 700;
}

.control-group { margin-bottom: 10px; }

.control-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.checkbox-group {
  padding: 10px 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
}

.checkbox-label {
  display: flex !important;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px !important;
  cursor: pointer;
  color: #f4efe4 !important;
}

.checkbox-label input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.control-help {
  font-size: 0.73rem;
  line-height: 1.45;
  color: var(--text-muted);
}

select {
  width: 100%;
  background: rgba(0, 0, 0, 0.28);
  color: #f4efe4;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 10px 8px;
  font-size: 16px;
  outline: none;
}

.btn {
  display: block;
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: 9px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s, box-shadow 0.15s;
  margin-bottom: 6px;
  -webkit-appearance: none;
  appearance: none;
  font-family: inherit;
}

.btn:last-child { margin-bottom: 0; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn.primary {
  background: linear-gradient(180deg, #e0c15a, var(--accent));
  color: var(--accent-text);
  box-shadow: 0 4px 14px rgba(201, 162, 39, 0.35);
}

.btn.secondary {
  background: rgba(255, 255, 255, 0.08);
  color: #f4efe4;
  border: 1px solid var(--panel-border);
}

#move-history {
  max-height: 160px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.8;
}

#move-history::-webkit-scrollbar { width: 4px; }
#move-history::-webkit-scrollbar-thumb {
  background: rgba(200, 180, 120, 0.35);
  border-radius: 2px;
}

.move-entry {
  display: flex;
  gap: 6px;
  padding: 1px 0;
  flex-wrap: wrap;
}

.move-num { color: var(--text-muted); min-width: 22px; }
.move-cho { color: #7ddeb8; }
.move-han { color: #ff8f8f; }

#captured-cho, #captured-han {
  font-size: 0.82rem;
  min-height: 24px;
  word-break: break-all;
  font-family: "Nanum Myeongjo", "Noto Serif KR", serif;
}

#captured-cho { color: var(--cho); margin-bottom: 4px; }
#captured-han { color: var(--han); }
.captured-label {
  color: var(--text-muted);
  margin-right: 4px;
  font-family: "Noto Sans KR", sans-serif;
}

#ai-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  min-height: 18px;
  margin-bottom: 8px;
  text-align: center;
}

#engine-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-bottom: 8px;
}

.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.08);
  color: #f4efe4;
}

.status-chip.hidden,
.warning-text.hidden {
  display: none;
}

#power-saver-badge {
  color: #c9f7c6;
  border-color: rgba(96, 200, 96, 0.35);
}

#stockfish-state {
  color: #ffe082;
  border-color: rgba(255, 208, 106, 0.35);
}

.warning-text {
  margin-bottom: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(220, 120, 40, 0.12);
  border: 1px solid rgba(220, 120, 40, 0.25);
  font-size: 0.74rem;
  line-height: 1.45;
  color: #ffd7a0;
}

#eval-bar-container { display: none; }

#eval-bar {
  height: 12px;
  background: var(--han);
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 4px;
}

#eval-fill {
  height: 100%;
  background: var(--cho);
  width: 50%;
  transition: width 0.4s ease;
}

#eval-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--text-muted);
}

#opensource-notice .notice-text {
  font-size: 0.78rem;
  line-height: 1.55;
  color: #f4efe4;
  margin-bottom: 8px;
}

#opensource-notice .notice-list {
  padding-left: 18px;
  color: var(--text-muted);
  font-size: 0.76rem;
  line-height: 1.55;
}

#opensource-notice a {
  color: #d8c06a;
  text-decoration: none;
}

#opensource-notice a:hover {
  text-decoration: underline;
}

#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 20, 14, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  backdrop-filter: blur(4px);
}

#modal-overlay.hidden { display: none; }

#modal {
  background: linear-gradient(180deg, #2a4a3a, #1a3228);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 32px 28px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
  animation: board-in 0.35s ease-out;
}

#modal h2 {
  font-family: "Noto Serif KR", serif;
  font-size: 1.4rem;
  margin-bottom: 12px;
  color: #f7efd8;
}

#modal p { color: var(--text-muted); margin-bottom: 24px; }

@media (max-width: 800px) {
  #main-layout {
    flex-direction: column;
    align-items: stretch;
  }

  aside#side-panel {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
  }

  .panel-section { flex: 1 1 160px; }
  #move-history { max-height: 120px; }
}

@media (max-width: 480px) {
  #app { padding: 8px 8px 16px; }

  header {
    flex-direction: column;
    align-items: flex-start;
  }

  #board-container {
    width: 100%;
    border-radius: 10px;
  }

  aside#side-panel { flex-direction: column; }
  .panel-section { flex: 1 1 auto; }
}

@media (display-mode: standalone) {
  body { background-color: var(--felt); }
}
