/* ============================================================
   짭몽어스 (Office Impostor) — Pixel/Office themed stylesheet
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&family=Noto+Sans+KR:wght@400;600;800&display=swap');

:root {
  --bg-dark: #1b2230;
  --bg-panel: #232b3d;
  --bg-panel-light: #2c3650;
  --accent: #f5c518;
  --accent-2: #38fedb;
  --danger: #e0473e;
  --success: #4caf6f;
  --text-main: #f0f2f7;
  --text-dim: #9aa4bd;
  --border-pixel: #0e1320;
  --radius: 10px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: -webkit-fill-available; /* iOS Safari 100vh bug fallback */
  background: var(--bg-dark);
  color: var(--text-main);
  font-family: 'Noto Sans KR', system-ui, sans-serif;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
}
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

body {
  background-image:
    linear-gradient(rgba(255,255,255,0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.02) 1px, transparent 1px);
  background-size: 24px 24px;
}

#root, #app {
  width: 100%;
  height: 100%;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  outline: none;
}
input {
  font-family: inherit;
}

.hidden { display: none !important; }

/* ---------- Screens ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
}
.screen.active { display: flex; }
.overlay-screen.active {
  background: rgba(10, 13, 22, 0.85);
  justify-content: center;
  z-index: 50;
}

/* ---------- Lobby ---------- */
#screen-lobby {
  justify-content: flex-start;
  padding: max(24px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
.lobby-header { text-align: center; margin-bottom: 18px; }
.game-logo {
  font-family: 'Press Start 2P', 'Noto Sans KR', sans-serif;
  font-size: 28px;
  color: var(--accent);
  text-shadow: 3px 3px 0 var(--border-pixel);
  margin: 0;
  letter-spacing: 1px;
}
.logo-emoji { filter: drop-shadow(2px 2px 0 #000); }
.game-subtitle {
  color: var(--text-dim);
  margin: 8px 0 0;
  font-size: 14px;
}

.panel {
  background: var(--bg-panel);
  border: 3px solid var(--border-pixel);
  border-radius: var(--radius);
  box-shadow: 0 6px 0 var(--border-pixel), 0 8px 16px rgba(0,0,0,0.4);
  padding: 20px;
  width: 100%;
  max-width: 420px;
  margin: 0 auto 16px;
}

.field-label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin: 12px 0 6px;
  font-weight: 600;
}
.text-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 8px;
  border: 2px solid #3a4666;
  background: #131926;
  color: var(--text-main);
  font-size: 16px;
}
.text-input:focus { border-color: var(--accent); }

.color-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}
.color-swatch {
  aspect-ratio: 1;
  border-radius: 8px;
  border: 3px solid transparent;
  cursor: pointer;
  position: relative;
  box-shadow: inset 0 -6px 0 rgba(0,0,0,0.25);
}
.color-swatch.selected { border-color: var(--accent); transform: scale(1.08); }
.color-swatch.taken { opacity: 0.3; cursor: not-allowed; }
.color-swatch.taken::after {
  content: '✕';
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 14px;
}

.btn {
  border-radius: 8px;
  font-weight: 800;
  transition: transform 0.05s ease, filter 0.15s ease;
}
.btn:active { transform: translateY(2px); }
.btn-primary {
  background: var(--accent);
  color: #1b2230;
  box-shadow: 0 4px 0 #a5860c;
}
.btn-primary:active { box-shadow: 0 1px 0 #a5860c; }
.btn-secondary {
  background: var(--bg-panel-light);
  color: var(--text-main);
  box-shadow: 0 4px 0 #12172a;
  border: 2px solid #3a4666;
}
.btn-lg { padding: 14px 20px; font-size: 16px; width: 100%; margin-top: 14px; min-height: 48px; }
.btn-sm { padding: 8px 14px; font-size: 13px; background: var(--accent-2); color: #08272a; border-radius: 6px; min-height: 36px; }

.error-text { color: var(--danger); font-size: 13px; min-height: 18px; margin: 8px 0 0; text-align: center; }
.hint-text { color: var(--text-dim); font-size: 12px; text-align: center; margin-top: 10px; }

.lobby-room { max-width: 480px; }
.room-title-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.room-title-row h2 { margin: 0; font-size: 18px; }
.player-count-badge {
  background: var(--bg-panel-light);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--accent-2);
  font-weight: 700;
}
.player-list {
  list-style: none;
  margin: 0; padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 40vh;
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
.player-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--bg-panel-light);
  border-radius: 8px;
  font-size: 14px;
}
.player-dot { width: 16px; height: 16px; border-radius: 4px; flex-shrink: 0; border: 2px solid rgba(0,0,0,0.4); }
.player-list .host-tag { color: var(--accent); font-size: 11px; margin-left: auto; font-weight: 800; }
.player-list .disconnected { opacity: 0.4; }

/* ---------- Game screen / HUD ---------- */
#screen-game { justify-content: flex-start; }
.hud-top {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
  background: linear-gradient(to bottom, rgba(10,13,22,0.9), rgba(10,13,22,0));
  z-index: 20;
  pointer-events: none;
  flex-wrap: wrap;
}
.hud-top > * { pointer-events: auto; }
.hud-left { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.role-badge {
  font-size: 12px;
  font-weight: 800;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--success);
  color: #06210f;
}
.role-badge.impostor { background: var(--danger); color: #2a0705; }
.task-progress {
  font-size: 12px;
  background: rgba(0,0,0,0.4);
  padding: 6px 10px;
  border-radius: 999px;
  color: var(--accent-2);
  font-weight: 700;
}
.room-label {
  font-size: 13px;
  background: rgba(0,0,0,0.45);
  padding: 6px 12px;
  border-radius: 999px;
  color: var(--text-main);
  font-weight: 700;
}
.btn-emergency {
  background: var(--danger);
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 3px 0 #8f1c17;
}

.game-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #10141f;
}
#gameCanvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;
}

.interact-hint {
  position: absolute;
  bottom: max(138px, calc(env(safe-area-inset-bottom) + 128px));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.7);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 25;
  white-space: nowrap;
  max-width: calc(100vw - 32px);
  text-overflow: ellipsis;
  overflow: hidden;
}

.btn-kill, .btn-report, .btn-task {
  position: absolute;
  right: max(20px, calc(env(safe-area-inset-right) + 14px));
  bottom: max(30px, calc(env(safe-area-inset-bottom) + 20px));
  width: 76px; height: 76px;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 800;
  z-index: 25;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  /* larger min touch target on touch devices, per mobile a11y guidance */
  touch-action: none;
}
.btn-kill { background: var(--danger); color: #fff; box-shadow: 0 4px 0 #8f1c17; }
.btn-report { background: #ffffff; color: #1b2230; box-shadow: 0 4px 0 #999; }
.btn-task { background: var(--accent); color: #1b2230; box-shadow: 0 4px 0 #a5860c; }

.joystick {
  position: absolute;
  left: max(26px, calc(env(safe-area-inset-left) + 16px));
  bottom: max(30px, calc(env(safe-area-inset-bottom) + 20px));
  width: 110px; height: 110px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 2px solid rgba(255,255,255,0.2);
  z-index: 25;
  touch-action: none;
}
.joystick-knob {
  position: absolute;
  width: 46px; height: 46px;
  border-radius: 50%;
  background: rgba(255,255,255,0.35);
  top: 32px; left: 32px;
  pointer-events: none;
}

/* ---------- Chat panel ---------- */
.chat-panel {
  position: absolute;
  right: max(12px, env(safe-area-inset-right));
  top: max(58px, calc(env(safe-area-inset-top) + 52px));
  width: min(260px, 62vw);
  max-height: 40vh;
  background: rgba(15,19,30,0.9);
  border-radius: 10px;
  border: 2px solid #3a4666;
  z-index: 22;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-panel.collapsed .chat-messages,
.chat-panel.collapsed .chat-form { display: none; }
.chat-toggle {
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  background: var(--bg-panel-light);
  min-height: 36px;
  display: flex;
  align-items: center;
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  padding: 8px 10px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.chat-msg .chat-name { font-weight: 800; margin-right: 4px; }
.chat-msg.system { color: var(--text-dim); font-style: italic; }
.chat-form { display: flex; gap: 6px; padding: 8px; border-top: 1px solid #3a4666; }
.chat-input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 6px;
  border: none;
  background: #131926;
  color: var(--text-main);
  /* 16px avoids iOS Safari auto-zoom-on-focus for text inputs */
  font-size: 16px;
  min-height: 36px;
}

/* ---------- Meeting overlay ---------- */
.meeting-panel {
  background: var(--bg-panel);
  border: 3px solid var(--border-pixel);
  border-radius: var(--radius);
  box-shadow: 0 8px 0 var(--border-pixel);
  padding: 20px;
  padding-bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px));
  width: 92%;
  max-width: 460px;
  max-height: min(90vh, 90dvh);
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}
.meeting-title { text-align: center; margin: 0 0 6px; color: var(--danger); }
.meeting-phase-label {
  text-align: center;
  font-size: 12px;
  font-weight: 800;
  color: var(--accent-2);
  margin-bottom: 2px;
}
.meeting-timer {
  text-align: center;
  font-family: 'Press Start 2P', sans-serif;
  font-size: 28px;
  color: var(--accent);
  margin-bottom: 14px;
}
.meeting-timer.discussion { color: var(--accent-2); }
.vote-list {
  list-style: none; margin: 0; padding: 0;
  display: flex; flex-direction: column; gap: 6px;
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  max-height: 30vh;
  margin-bottom: 10px;
}
.vote-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px;
  min-height: 44px;
  background: var(--bg-panel-light);
  border-radius: 8px;
  cursor: pointer;
  border: 2px solid transparent;
}
.vote-item.dead { opacity: 0.35; cursor: not-allowed; }
.vote-item.voted-for-me { border-color: var(--accent); }
.vote-item .vote-count { margin-left: auto; font-weight: 800; color: var(--accent-2); font-size: 12px; }
.meeting-chat { max-height: 16vh; margin: 6px 0; background: rgba(0,0,0,0.2); border-radius: 8px; }

/* ---------- End screen ---------- */
.end-panel {
  background: var(--bg-panel);
  border: 3px solid var(--border-pixel);
  border-radius: var(--radius);
  box-shadow: 0 8px 0 var(--border-pixel);
  padding: 26px;
  padding-bottom: max(26px, calc(env(safe-area-inset-bottom) + 16px));
  width: 92%;
  max-width: 420px;
  max-height: min(90vh, 90dvh);
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  text-align: center;
}
.end-title { font-family: 'Press Start 2P', sans-serif; font-size: 20px; margin: 0 0 8px; }
.end-title.crew-win { color: var(--success); }
.end-title.impostor-win { color: var(--danger); }
.end-reason { color: var(--text-dim); margin: 0 0 16px; font-size: 14px; }
.end-role-list { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; gap: 6px; max-height: 30vh; overflow-y: auto; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
.end-role-list li { display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--bg-panel-light); border-radius: 6px; font-size: 13px; }
.end-role-list .role-tag { margin-left: auto; font-weight: 800; font-size: 11px; padding: 2px 8px; border-radius: 999px; }
.end-role-list .role-tag.crew { background: var(--success); color: #06210f; }
.end-role-list .role-tag.impostor { background: var(--danger); color: #2a0705; }

/* ---------- Task modal ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(5,7,12,0.85);
  z-index: 60;
  display: flex; align-items: center; justify-content: center;
}
.task-modal-content {
  background: var(--bg-panel);
  border: 3px solid var(--border-pixel);
  border-radius: var(--radius);
  padding: 22px;
  width: 92%;
  max-width: 380px;
  max-height: min(85vh, 85dvh);
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  text-align: center;
}
.task-modal-content h3 { margin: 0 0 12px; }
.task-close { margin-top: 14px; }

/* wiring task */
.wire-row { display: flex; justify-content: space-between; margin: 16px 0; }
.wire-col { display: flex; flex-direction: column; gap: 14px; }
.wire-dot {
  width: 30px; height: 30px; border-radius: 50%;
  border: 3px solid #fff;
  cursor: pointer;
}
.wire-dot.done { opacity: 0.3; cursor: default; }

/* sorting task */
.sort-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 14px 0; }
.sort-item {
  aspect-ratio: 1;
  border-radius: 6px;
  cursor: pointer;
  font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-panel-light);
  border: 2px solid #3a4666;
}
.sort-item.matched { opacity: 0.25; cursor: default; }
.sort-item.wrong { animation: shake 0.3s; border-color: var(--danger); }
@keyframes shake { 0%,100%{transform:translateX(0);} 25%{transform:translateX(-4px);} 75%{transform:translateX(4px);} }

/* click-timing task (coffee / printer / stamp) */
.timing-bar { position: relative; height: 30px; background: #131926; border-radius: 999px; margin: 18px 0; overflow: hidden; }
.timing-target { position: absolute; top: 0; height: 100%; background: rgba(76,175,111,0.5); }
.timing-cursor { position: absolute; top: 0; width: 4px; height: 100%; background: var(--accent); }
.timing-progress { font-size: 13px; color: var(--text-dim); margin-bottom: 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: max(20px, calc(env(safe-area-inset-bottom) + 10px));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.85);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 100;
  max-width: 90%;
  text-align: center;
}

/* ---------- Responsive breakpoints ---------- */

/* Larger screens (tablet/desktop): move touch controls further from the edge */
@media (min-width: 700px) {
  .btn-kill, .btn-report, .btn-task { right: 40px; bottom: 40px; }
  .joystick { left: 40px; bottom: 40px; }
}

/* Small phones (portrait, narrow width e.g. iPhone SE ~375px and below):
   shrink touch controls a bit and tighten HUD/chat spacing so nothing overlaps. */
@media (max-width: 420px) {
  .game-logo { font-size: 22px; }
  .panel { padding: 16px; }

  .hud-top { padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .role-badge, .task-progress, .room-label { font-size: 11px; padding: 5px 8px; }
  .btn-emergency { font-size: 12px; padding: 7px 10px; }

  .chat-panel { width: min(200px, 58vw); top: max(52px, calc(env(safe-area-inset-top) + 46px)); max-height: 34vh; }
  .chat-toggle { font-size: 11px; padding: 6px 10px; }

  .joystick { width: 96px; height: 96px; left: max(16px, calc(env(safe-area-inset-left) + 8px)); bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px)); }
  .joystick-knob { width: 40px; height: 40px; top: 28px; left: 28px; }

  .btn-kill, .btn-report, .btn-task {
    width: 62px; height: 62px;
    font-size: 10px;
    right: max(14px, calc(env(safe-area-inset-right) + 8px));
    bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px));
  }
  .interact-hint { bottom: max(112px, calc(env(safe-area-inset-bottom) + 100px)); font-size: 12px; }
}

/* Short viewports (landscape phones, e.g. height <= 420px): keep the
   vertical stack (joystick/buttons) from crowding the HUD bar. */
@media (max-height: 420px) and (orientation: landscape) {
  .btn-kill, .btn-report, .btn-task { width: 58px; height: 58px; font-size: 10px; bottom: 12px; }
  .joystick { width: 88px; height: 88px; bottom: 12px; }
  .joystick-knob { width: 36px; height: 36px; top: 26px; left: 26px; }
  .chat-panel { max-height: 60vh; top: 6px; }
  .interact-hint { bottom: 96px; }
  .meeting-panel, .end-panel, .task-modal-content { padding: 14px; max-height: 92vh; }
}

/* Fine-grained pointer-less (touch) devices: use pan-y instead of blocking
   all touch on the html/body, but keep the joystick/canvas non-scrollable. */
@media (hover: none) and (pointer: coarse) {
  .color-swatch { min-height: 40px; }
}
