/* ==========================================================================
   Balance It — Commercial V2 Stylesheet
   Tactile craftsmanship aesthetic with responsive frame, accessible controls & smooth transitions.
   ========================================================================== */

/* ---------- CSS Reset & Global Variables ---------- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

:root {
  /* Color Palette */
  --bg-dark: #1f140e;
  --parchment: #faf4e6;
  --parchment-dark: #eee3cb;
  --parchment-deep: #decfa8;
  
  --oak-light: #ab794b;
  --oak: #8a572c;
  --oak-dark: #5e371a;
  --oak-deep: #381d0b;
  
  --brass-light: #f5d47a;
  --brass: #c99a24;
  --brass-dark: #826010;
  
  --ink: #2b1d12;
  --ink-soft: #6d5543;
  --ink-muted: #967d6a;

  --accent-red: #c93b2b;
  --accent-green: #3b8a47;
  --accent-blue: #337ab7;
  --accent-gold: #e5a919;

  /* Typography */
  --font-serif: 'Fraunces', Georgia, serif;
  --font-sans: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', Consolas, Monaco, monospace;

  /* Layout & Geometry */
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 8px;
  --shadow-frame: 0 25px 60px rgba(10, 5, 2, 0.6), 0 5px 15px rgba(10, 5, 2, 0.4);
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-sans);
  background:
    radial-gradient(ellipse 130% 90% at 50% -10%, #4a2f18 0%, transparent 60%),
    radial-gradient(ellipse 120% 100% at 50% 115%, #180d06 0%, transparent 65%),
    linear-gradient(165deg, #2a1a0e 0%, #170d06 60%, #0d0603 100%);
  color: var(--ink);
}

/* ---------- Master App Wrapper ---------- */
#app {
  width: 100vw;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: min(2vh, 12px);
}

#frame {
  position: relative;
  width: min(940px, 98vw);
  height: min(96dvh, 740px);
  padding: 12px;
  border-radius: 22px;
  background: linear-gradient(155deg, var(--oak-light) 0%, var(--oak) 40%, var(--oak-dark) 75%, var(--oak-deep) 100%);
  box-shadow: var(--shadow-frame), inset 0 2px 2px rgba(255, 255, 255, 0.2), inset 0 -6px 14px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
}

#frame::before {
  content: '';
  position: absolute;
  inset: 5px;
  border-radius: 16px;
  pointer-events: none;
  z-index: 1;
  box-shadow: inset 0 0 0 2px rgba(255, 220, 140, 0.55), inset 0 0 0 3px rgba(0, 0, 0, 0.3);
}

/* ---------- Screen Architecture ---------- */
.screen {
  position: absolute;
  inset: 12px;
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transform: scale(0.985);
  transition: opacity 0.26s ease, transform 0.26s ease;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
  transform: scale(1);
  z-index: 2;
}

/* ==========================================================================
   SCREEN 1: MAIN MENU
   ========================================================================== */
#screen-menu {
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 18%, #fffdf8 0%, var(--parchment) 50%, var(--parchment-dark) 100%);
}

.brand-title {
  font-family: var(--font-serif);
  font-size: clamp(2.4rem, 7.5vw, 3.8rem);
  font-weight: 700;
  color: var(--ink);
  line-height: 1.1;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.6), 0 3px 10px rgba(100, 60, 20, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.brand-icon {
  font-size: 0.9em;
  color: var(--oak-dark);
  filter: drop-shadow(0 3px 6px rgba(60, 30, 10, 0.25));
  animation: brandSway 3.6s ease-in-out infinite;
  transform-origin: 50% 20%;
}

@keyframes brandSway {
  0%, 100% { transform: rotate(-4deg); }
  50% { transform: rotate(4deg); }
}

.brand-sub {
  font-size: clamp(0.85rem, 2.2vw, 1.05rem);
  font-weight: 800;
  color: var(--ink-soft);
  letter-spacing: 3px;
  text-transform: uppercase;
  margin-top: 6px;
}

.brand-sub::before, .brand-sub::after {
  content: '✦';
  color: var(--brass);
  font-size: 0.7em;
  margin: 0 8px;
  vertical-align: middle;
}

.menu-stats-row {
  margin-top: 18px;
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.stat-chip {
  background: linear-gradient(180deg, var(--oak-light), var(--oak-dark));
  color: #fff9ed;
  font-weight: 800;
  font-size: 0.9rem;
  padding: 8px 18px;
  border-radius: 30px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 3px 0 var(--oak-deep), 0 5px 12px rgba(60, 30, 10, 0.3);
}

.menu-buttons {
  margin-top: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(320px, 80vw);
}

.corner-btn {
  position: absolute;
  top: 16px;
  z-index: 10;
  background: linear-gradient(180deg, #fffdf8, var(--parchment-dark));
  border: 1.5px solid #d8c79c;
  color: var(--ink);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 3px 0 #c0b085, 0 6px 12px rgba(80, 50, 20, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease;
}

.corner-btn:active {
  transform: translateY(3px);
  box-shadow: none;
}

#menuSoundToggle { right: 18px; }
#menuMusicToggle { right: 68px; }
#settingsBtnMenu { left: 18px; }

.menu-footer {
  position: absolute;
  bottom: 12px;
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--ink-soft);
  letter-spacing: 0.5px;
}

/* ==========================================================================
   SCREEN 2: LEVEL SELECT
   ========================================================================== */
#screen-levels {
  padding: 20px 24px;
  background: linear-gradient(180deg, #fffefb, var(--parchment) 60%);
}

.levels-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.panel-title {
  font-family: var(--font-serif);
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 10px;
}

.chapters-scroll {
  flex: 1;
  overflow-y: auto;
  padding-right: 6px;
}

.chapter-card {
  background: linear-gradient(180deg, #fffdf8, var(--parchment-dark));
  border: 1.5px solid #ddcd9f;
  border-radius: var(--radius-md);
  padding: 14px 16px;
  margin-bottom: 16px;
  box-shadow: 0 3px 8px rgba(80, 50, 20, 0.08);
}

.chapter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.chapter-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chapter-pill {
  color: #ffffff;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 0.76rem;
  padding: 4px 10px;
  border-radius: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.chapter-name {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--ink);
}

.chapter-sub {
  font-size: 0.78rem;
  color: var(--ink-soft);
  margin-top: 2px;
}

.chapter-stars-pill {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 0.82rem;
  color: var(--brass-dark);
  background: rgba(255, 235, 160, 0.4);
  border: 1px solid var(--brass);
  padding: 4px 10px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 8px;
}

.lvl-btn {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  border: 1.5px solid #d4c295;
  background: linear-gradient(180deg, #fffcf5, var(--parchment-dark));
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--ink);
  box-shadow: 0 3px 0 #c2b184, 0 4px 8px rgba(70, 45, 15, 0.1);
  transition: transform 0.08s ease;
}

.lvl-btn:active {
  transform: translateY(3px);
  box-shadow: none;
}

.lvl-btn.locked {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

.lvl-btn.current {
  background: linear-gradient(180deg, #fff8e1, #ffecb3);
  border-color: var(--brass);
  box-shadow: 0 3px 0 var(--brass-dark), 0 0 0 2px rgba(201, 154, 36, 0.35);
}

.lvl-btn.signature {
  border-color: var(--brass);
  position: relative;
}

.lvl-btn.signature::before {
  content: '★';
  position: absolute;
  top: -6px;
  right: -5px;
  font-size: 0.6rem;
  color: var(--brass-dark);
  background: var(--brass-light);
  border-radius: 50%;
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--brass);
}

.lvl-stars-row {
  font-size: 0.55rem;
  letter-spacing: 1px;
  color: var(--brass);
  margin-top: 2px;
}

/* ==========================================================================
   SCREEN 3: HOW TO PLAY
   ========================================================================== */
#screen-howto {
  padding: 24px;
  background: linear-gradient(180deg, #fffefb, var(--parchment) 60%);
}

.howto-scroll {
  flex: 1;
  overflow-y: auto;
  padding-right: 6px;
}

.howto-card {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  background: linear-gradient(180deg, #fffdf8, var(--parchment-dark));
  border: 1.5px solid #ded0a6;
  border-radius: var(--radius-md);
  padding: 14px 18px;
  margin-bottom: 12px;
  box-shadow: 0 2px 6px rgba(80, 50, 20, 0.06);
}

.howto-num {
  font-family: var(--font-mono);
  font-size: 1.2rem;
  font-weight: 700;
  color: #fff9ed;
  background: linear-gradient(180deg, var(--oak-light), var(--oak-dark));
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 0 var(--oak-deep);
}

.howto-text {
  font-size: 0.92rem;
  color: var(--ink);
  line-height: 1.5;
}

.howto-text b {
  color: var(--oak-dark);
}

/* ==========================================================================
   SCREEN 4: GAMEPLAY
   ========================================================================== */
#screen-game {
  padding: 10px 12px;
  background: linear-gradient(180deg, #ece1c5, #decfa7);
}

#canvasContainer {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  box-shadow: inset 0 0 0 2px #d8c79c, 0 4px 14px rgba(60, 35, 10, 0.15);
  touch-action: none;
  overflow: hidden;
  background: #000000;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  aspect-ratio: 900 / 620;
  touch-action: none;
}

/* HUD Controls */
#gameHud {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 4px 4px 4px;
}

.hud-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.hud-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.hud-chip {
  background: linear-gradient(180deg, #fffdf8, var(--parchment-dark));
  border: 1.5px solid #d8c79c;
  border-radius: 24px;
  padding: 6px 14px;
  font-size: 0.82rem;
  font-weight: 800;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 2px 5px rgba(80, 50, 20, 0.1);
}

.hud-chip.badge {
  background: linear-gradient(180deg, var(--oak-light), var(--oak-dark));
  color: #fff9ed;
  border: none;
  box-shadow: 0 2px 5px rgba(60, 30, 10, 0.35);
}

.icon-btn {
  background: linear-gradient(180deg, #fffdf8, var(--parchment-dark));
  border: 1.5px solid #d8c79c;
  border-radius: 24px;
  padding: 8px 12px;
  min-width: 42px;
  min-height: 42px;
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 3px 0 #c2b184, 0 4px 8px rgba(70, 45, 15, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: transform 0.08s ease;
}

.icon-btn:active {
  transform: translateY(3px);
  box-shadow: none;
}

.icon-btn:disabled {
  opacity: 0.4;
  pointer-events: none;
  box-shadow: none;
  transform: translateY(2px);
}

.icon-btn.power {
  background: linear-gradient(180deg, #fff8e1, #ffecb3);
  border-color: var(--brass);
  box-shadow: 0 3px 0 var(--brass-dark), 0 4px 10px rgba(201, 154, 36, 0.25);
}

.power-count {
  font-family: var(--font-mono);
  font-size: 0.85rem;
}

/* ==========================================================================
   BUTTON DESIGN SYSTEM
   ========================================================================== */
.btn {
  font-family: var(--font-sans);
  font-weight: 800;
  font-size: 1.05rem;
  padding: 14px 22px;
  border-radius: var(--radius-md);
  border: none;
  cursor: pointer;
  position: relative;
  letter-spacing: 0.3px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #fff9ed;
  transition: transform 0.08s ease, filter 0.15s ease;
}

.btn:active {
  transform: translateY(3px);
}

.btn-primary {
  background: linear-gradient(180deg, var(--oak-light), var(--oak) 55%, var(--oak-dark));
  box-shadow: 0 5px 0 var(--oak-deep), 0 8px 18px rgba(50, 25, 10, 0.35);
}

.btn-primary:active {
  box-shadow: 0 2px 0 var(--oak-deep);
}

.btn-secondary {
  background: linear-gradient(180deg, var(--brass-light), var(--brass) 60%, var(--brass-dark));
  color: var(--oak-deep);
  box-shadow: 0 5px 0 #6e500c, 0 8px 18px rgba(110, 80, 15, 0.3);
}

.btn-secondary:active {
  box-shadow: 0 2px 0 #6e500c;
}

.btn-ghost {
  background: linear-gradient(180deg, #fffdf8, var(--parchment-dark));
  color: var(--ink);
  border: 1.5px solid #d4c295;
  box-shadow: 0 5px 0 #c2b184, 0 6px 14px rgba(70, 45, 15, 0.12);
}

.btn-ghost:active {
  box-shadow: 0 2px 0 #c2b184;
}

.btn-small {
  padding: 10px 18px;
  font-size: 0.88rem;
  border-radius: var(--radius-sm);
}

/* ==========================================================================
   MODAL OVERLAYS
   ========================================================================== */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 35%, rgba(40, 25, 10, 0.65), rgba(15, 8, 3, 0.82));
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 30;
  border-radius: 14px;
}

.modal-overlay.show {
  display: flex;
  animation: modalFade 0.22s ease forwards;
}

@keyframes modalFade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-card {
  background: linear-gradient(180deg, #fffefb, var(--parchment));
  border-radius: var(--radius-lg);
  padding: 28px 34px;
  text-align: center;
  max-width: 88%;
  width: 440px;
  box-shadow: 0 24px 50px rgba(10, 5, 2, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.8), 0 0 0 1.5px #ded0a6;
  animation: cardPop 0.28s cubic-bezier(0.2, 1.3, 0.4, 1) forwards;
}

@keyframes cardPop {
  from { opacity: 0; transform: scale(0.9) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-card h2 {
  font-family: var(--font-serif);
  color: var(--ink);
  font-size: 1.85rem;
  margin-bottom: 8px;
}

.modal-card p {
  color: var(--ink-soft);
  font-size: 0.92rem;
  margin-bottom: 18px;
  line-height: 1.5;
  font-weight: 600;
}

.modal-stars {
  font-size: 2.5rem;
  color: var(--brass);
  letter-spacing: 6px;
  margin-bottom: 12px;
  text-shadow: 0 2px 6px rgba(201, 154, 36, 0.35);
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

/* Settings List */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 16px 0 22px 0;
  text-align: left;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.5);
  border: 1px solid #ded0a6;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
}

.settings-label {
  font-weight: 800;
  font-size: 0.95rem;
  color: var(--ink);
}

.toggle-btn {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 0.85rem;
  padding: 6px 14px;
  border-radius: 16px;
  border: 1px solid var(--brass);
  background: var(--brass-light);
  color: var(--oak-deep);
  cursor: pointer;
}

/* Toast System */
.toast {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  background: linear-gradient(180deg, var(--oak-light), var(--oak-dark));
  color: #fff9ed;
  font-weight: 800;
  font-size: 0.84rem;
  padding: 8px 20px;
  border-radius: 24px;
  opacity: 0;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  transition: opacity 0.22s ease, transform 0.22s ease;
  pointer-events: none;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.good {
  background: linear-gradient(180deg, var(--accent-green), #2c6835);
}

/* ==========================================================================
   SVG ICONS
   ========================================================================== */
.icon-svg {
  width: 1.2em;
  height: 1.2em;
  display: inline-block;
  vertical-align: -0.18em;
  fill: currentColor;
  flex-shrink: 0;
}

.icon-svg.sm {
  width: 0.95em;
  height: 0.95em;
}

/* ==========================================================================
   RESPONSIVE DESIGN (Mobile & Tablet adaptivity)
   ========================================================================== */
@media (max-width: 580px) {
  #frame {
    border-radius: 18px;
    padding: 8px;
  }
  .screen {
    inset: 8px;
  }
  .brand-title {
    font-size: 2.3rem;
  }
  .level-grid {
    grid-template-columns: repeat(5, 1fr);
  }
  .hud-chip {
    font-size: 0.74rem;
    padding: 5px 10px;
  }
  .icon-btn {
    min-width: 36px;
    min-height: 36px;
    padding: 6px 8px;
  }
  .corner-btn {
    width: 38px;
    height: 38px;
  }
}

@media (max-height: 620px) {
  .brand-title {
    font-size: 2rem;
  }
  .menu-buttons {
    margin-top: 14px;
    gap: 8px;
  }
  .btn {
    padding: 10px 16px;
    font-size: 0.9rem;
  }
}
