/* ==================== Origami 2048 — Papercraft UI ==================== */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --ink:        #3A2E20;
  --ink-soft:   #5A4A38;
  --paper:      #FAF6EC;
  --paper-2:    #F1E9D2;
  --paper-3:    #E8DCBE;
  --washi:      #F4ECDC;
  --crane-red:  #B23A30;
  --kabuto:     #2A4D6E;
  --tulip:      #C97D7D;
  --hanada:     #3A7A99;
  --yamabuki:   #F4C946;
  --asagi:      #7BBED1;
  --bara:       #D87F4F;
  --gold:       #C99B3F;
  --gold-soft:  #E2C16A;
  --shadow:     rgba(58, 46, 32, 0.18);
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  font-family: 'Fredoka', 'Noto Sans SC', system-ui, sans-serif;
  color: var(--ink);
  font-weight: 600;
  /* dynamic viewport units handle iOS safari address-bar height changes */
  position: fixed;
  inset: 0;
}

#gameContainer {
  position: fixed;
  inset: 0;
  z-index: 1;
}
#gameContainer canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* ==================== Flash & Shake ==================== */
.flash-overlay {
  position: fixed; inset: 0;
  pointer-events: none; z-index: 50;
  background: rgba(255,255,255,0);
  transition: background 0.15s ease;
}
.flash-overlay.flash-white { background: rgba(255, 252, 240, 0.85); }
.flash-overlay.flash-gold  { background: rgba(244, 201, 70, 0.35); }

.shake { animation: shake 0.34s cubic-bezier(.36,.07,.19,.97) both; }
@keyframes shake {
  10%, 90% { transform: translate3d(-3px, 0, 0); }
  20%, 80% { transform: translate3d(5px, 0, 0); }
  30%, 50%, 70% { transform: translate3d(-9px, 1px, 0); }
  40%, 60% { transform: translate3d(9px, -1px, 0); }
}

/* ==================== Top Bar ==================== */
.topbar {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 30;
  display: flex; gap: 10px;
}

.icon-btn {
  width: 44px; height: 44px;
  border-radius: 12px;
  border: 2px solid var(--ink);
  background: var(--ink);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--ink), 0 8px 16px var(--shadow);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  position: relative;
  padding: 0;
}
.icon-btn:hover { transform: translateY(-2px); box-shadow: 0 5px 0 var(--ink), 0 10px 20px var(--shadow); }
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink), 0 4px 8px var(--shadow); }
.icon-btn .ic { width: 22px; height: 22px; }
.lang-btn {
  width: auto; min-width: 44px; padding: 0 10px;
  font-family: 'Lilita One', sans-serif;
  font-size: 16px; letter-spacing: 1px;
}

/* pause/play swap */
.icon-btn .ic-play { display: none; }
.icon-btn.is-paused .ic-pause { display: none; }
.icon-btn.is-paused .ic-play { display: block; }

/* mute swap */
.icon-btn .ic-mute { display: none; }
.icon-btn.is-muted .ic-sound { display: none; }
.icon-btn.is-muted .ic-mute { display: block; }

/* ==================== Score HUD ==================== */
.score-hud {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  left: max(14px, env(safe-area-inset-left));
  z-index: 30;
  display: flex; gap: 10px;
}
.score-card {
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 12px;
  padding: 6px 14px;
  box-shadow: 0 3px 0 var(--ink), 0 8px 16px var(--shadow);
  min-width: 76px;
  text-align: center;
}
.score-card.best { background: var(--yamabuki); }
.score-label {
  font-size: 10px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-weight: 700;
}
.score-value {
  font-family: 'Lilita One', 'Fredoka', sans-serif;
  font-size: 22px;
  line-height: 1.1;
  color: var(--ink);
  font-weight: 800;
}

/* ==================== Overlays ==================== */
.overlay {
  position: fixed; inset: 0;
  z-index: 40;
  display: flex; align-items: center; justify-content: center;
  background: rgba(58, 46, 32, 0.18);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  padding: 20px;
  animation: fadeIn 0.25s ease;
}
.overlay.hidden { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.ready-card, .gameover-card, .win-card, .pause-card {
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 18px;
  padding: 28px 32px;
  text-align: center;
  box-shadow: 0 6px 0 var(--ink), 0 18px 40px rgba(58, 46, 32, 0.3);
  position: relative;
  max-width: 92vw;
  /* deckle edge feel via inner shadow */
  background-image:
    radial-gradient(circle at 12% 88%, rgba(58, 46, 32, 0.05) 0%, transparent 12%),
    radial-gradient(circle at 88% 12%, rgba(58, 46, 32, 0.04) 0%, transparent 12%);
}
.ready-card::before,
.gameover-card::before,
.win-card::before,
.pause-card::before {
  content: '';
  position: absolute;
  inset: 8px;
  border: 1px dashed rgba(58, 46, 32, 0.25);
  border-radius: 12px;
  pointer-events: none;
}

/* Ready card */
.brand-stack { margin-bottom: 16px; }
.brand-title {
  font-family: 'Noto Serif SC', serif;
  font-size: 44px;
  font-weight: 800;
  color: var(--ink);
  letter-spacing: 4px;
  line-height: 1.05;
}
.brand-sub {
  font-family: 'Lilita One', sans-serif;
  font-size: 16px;
  letter-spacing: 4px;
  color: var(--ink-soft);
  margin-top: 4px;
  text-transform: uppercase;
}
.ready-msg {
  font-size: 14px;
  color: var(--ink-soft);
  margin-bottom: 18px;
  max-width: 320px;
  line-height: 1.5;
}
.ready-hint {
  font-size: 14px;
  color: var(--ink-soft);
  margin: 14px 0 18px 0;
  display: flex; flex-wrap: wrap; gap: 6px;
  align-items: center; justify-content: center;
}
.key {
  display: inline-block;
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 6px;
  padding: 2px 8px;
  font-family: 'Lilita One', sans-serif;
  font-size: 12px;
  color: var(--ink);
  box-shadow: 0 2px 0 var(--ink);
  min-width: 24px;
  text-align: center;
}

/* Size picker */
.size-picker {
  margin: 14px 0 18px 0;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.size-label {
  font-size: 11px;
  letter-spacing: 2px;
  color: var(--ink-soft);
  text-transform: uppercase;
}
.size-btns {
  display: flex; gap: 8px;
}
.size-btn {
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 10px;
  padding: 6px 14px;
  cursor: pointer;
  font-family: 'Lilita One', sans-serif;
  font-size: 16px;
  color: var(--ink);
  box-shadow: 0 2px 0 var(--ink);
  transition: all 0.15s ease;
}
.size-btn:hover { transform: translateY(-1px); box-shadow: 0 3px 0 var(--ink); }
.size-btn.active {
  background: var(--yamabuki);
  box-shadow: 0 2px 0 var(--ink), inset 0 0 0 1px rgba(255,255,255,0.4);
}

/* ==================== Buttons ==================== */
.btn {
  border: 2px solid var(--ink);
  border-radius: 12px;
  padding: 11px 26px;
  font-family: 'Lilita One', 'Fredoka', sans-serif;
  font-size: 18px;
  letter-spacing: 1.5px;
  cursor: pointer;
  transition: all 0.12s ease;
  margin: 4px 4px;
  display: inline-block;
}
.btn.primary {
  background: var(--yamabuki);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink), 0 8px 16px var(--shadow);
}
.btn.primary:hover { transform: translateY(-2px); box-shadow: 0 6px 0 var(--ink), 0 10px 20px var(--shadow); }
.btn.primary:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--ink); }
.btn.ghost {
  background: var(--paper-2);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink), 0 8px 16px var(--shadow);
}
.btn.ghost:hover { transform: translateY(-2px); box-shadow: 0 6px 0 var(--ink), 0 10px 20px var(--shadow); }
.btn.ghost:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--ink); }
.btn-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 6px;
  flex-wrap: wrap;
}

/* ==================== Game Over Card ==================== */
.gameover-title, .win-title {
  font-family: 'Noto Serif SC', serif;
  font-size: 32px;
  font-weight: 800;
  color: var(--crane-red);
  letter-spacing: 3px;
  margin-bottom: 14px;
}
.win-title { color: var(--gold); text-shadow: 0 2px 0 var(--ink); }
.win-sub {
  font-size: 14px;
  color: var(--ink-soft);
  margin-bottom: 18px;
  max-width: 320px;
  line-height: 1.5;
}

.score-panel {
  display: flex; align-items: center; justify-content: center;
  gap: 18px; margin: 12px 0 20px 0;
  position: relative;
}
.score-rows {
  display: flex; flex-direction: column; gap: 8px; align-items: stretch;
  min-width: 200px;
}
.score-row {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 10px;
  padding: 8px 14px;
  min-width: 200px;
  box-shadow: 0 2px 0 var(--ink);
}
.score-row .label {
  font-size: 11px; letter-spacing: 2px; text-transform: uppercase;
  color: var(--ink-soft);
}
.score-row .value {
  font-family: 'Lilita One', sans-serif;
  font-size: 22px;
  color: var(--ink);
}
.new-best {
  position: absolute;
  top: -16px; right: -8px;
  background: var(--crane-red);
  color: #fff;
  padding: 4px 12px;
  border-radius: 100px;
  font-family: 'Lilita One', sans-serif;
  font-size: 13px;
  letter-spacing: 1.5px;
  border: 2px solid var(--ink);
  box-shadow: 0 2px 0 var(--ink);
  transform: rotate(8deg);
  animation: bounce 0.6s ease;
}
.new-best.hidden { display: none; }
@keyframes bounce {
  0% { transform: rotate(8deg) scale(0); }
  50% { transform: rotate(8deg) scale(1.2); }
  100% { transform: rotate(8deg) scale(1); }
}

/* Pause card */
.pause-card { padding: 32px 36px; }
.pause-title {
  font-family: 'Noto Serif SC', serif;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 4px;
  color: var(--ink);
  margin-bottom: 18px;
}

/* ==================== Loading ==================== */
.loading {
  position: fixed; inset: 0;
  background: var(--paper);
  z-index: 100;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px;
  transition: opacity 0.4s ease;
}
.loading.hidden { opacity: 0; pointer-events: none; }
.loading-bar {
  width: 240px; height: 8px;
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 100px;
  overflow: hidden;
}
.loading-fill {
  height: 100%; width: 0%;
  background: var(--yamabuki);
  border-right: 2px solid var(--ink);
  transition: width 0.25s ease;
}
.loading-text {
  font-family: 'Noto Serif SC', serif;
  font-size: 16px;
  letter-spacing: 4px;
  color: var(--ink-soft);
}

/* ==================== Home Link ==================== */
.home-link {
  position: fixed;
  bottom: max(14px, env(safe-area-inset-bottom));
  left: max(14px, env(safe-area-inset-left));
  z-index: 30;
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 2px solid var(--ink);
  background: var(--paper-2);
  color: var(--ink);
  display: flex; align-items: center; justify-content: center;
  text-decoration: none;
  box-shadow: 0 3px 0 var(--ink), 0 6px 12px var(--shadow);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.home-link:hover { transform: translateY(-2px); box-shadow: 0 5px 0 var(--ink), 0 8px 16px var(--shadow); }
.home-link:active { transform: translateY(1px); box-shadow: 0 2px 0 var(--ink); }
.home-link svg { width: 22px; height: 22px; }

/* ==================== Mobile Tweaks ==================== */
@media (max-width: 540px) {
  .brand-title { font-size: 34px; letter-spacing: 3px; }
  .brand-sub { font-size: 13px; letter-spacing: 3px; }
  .gameover-title, .win-title { font-size: 26px; }
  .ready-card, .gameover-card, .win-card, .pause-card { padding: 22px 20px; }
  .btn { font-size: 16px; padding: 10px 22px; }
  .score-card { padding: 4px 10px; min-width: 56px; }
  .score-value { font-size: 17px; }
  .score-label { font-size: 9px; letter-spacing: 1.2px; }
  .icon-btn { width: 36px; height: 36px; border-radius: 10px; }
  .icon-btn .ic { width: 18px; height: 18px; }
  .lang-btn { font-size: 13px; padding: 0 7px; }
  .topbar { gap: 6px; top: max(10px, env(safe-area-inset-top)); right: max(8px, env(safe-area-inset-right)); }
  .score-hud { gap: 6px; top: max(10px, env(safe-area-inset-top)); left: max(8px, env(safe-area-inset-left)); }
  .home-link { width: 44px; height: 44px; }
  .home-link svg { width: 20px; height: 20px; }
}

/* narrow phones (≤420px): stack score HUD below topbar so both fit cleanly */
@media (max-width: 420px) {
  .score-hud {
    top: calc(max(10px, env(safe-area-inset-top)) + 50px);
    left: 50%;
    transform: translateX(-50%);
  }
  .icon-btn { width: 34px; height: 34px; }
  .icon-btn .ic { width: 17px; height: 17px; }
}

/* iPhone SE / very narrow phones (≤340px): even tighter buttons */
@media (max-width: 340px) {
  .topbar { gap: 4px; }
  .icon-btn { width: 30px; height: 30px; }
  .icon-btn .ic { width: 15px; height: 15px; }
  .score-card { padding: 3px 8px; min-width: 48px; }
  .score-value { font-size: 15px; }
}

/* short landscape phones — keep board visible by shrinking HUD vertically */
@media (max-height: 480px) and (orientation: landscape) {
  .topbar { gap: 6px; top: max(8px, env(safe-area-inset-top)); }
  .score-hud { gap: 6px; top: max(8px, env(safe-area-inset-top)); }
  .icon-btn { width: 34px; height: 34px; }
  .icon-btn .ic { width: 17px; height: 17px; }
  .score-card { padding: 3px 8px; min-width: 50px; }
  .score-value { font-size: 15px; }
  .home-link { width: 40px; height: 40px; }
}
