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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0000aa;
  font-family: 'Courier New', Courier, monospace;
  color: #ffffff;
}

.screen {
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 18px 24px;
}

/* ── HEADER BOX ── */
.header-box {
  border: 2px solid #ffffff;
}

.box-title {
  display: block;
  width: 100%;
  padding: 8px 0 4px;
  color: #ffff55;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(4px, 1.1vw, 8px);
  line-height: 1.2;
  white-space: pre;
  text-align: center;
  border-bottom: 1px solid #225588;
  letter-spacing: 0;
  overflow: hidden;
  box-sizing: border-box;
}

.header-inner {
  padding: 14px 40px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.header-title {
  font-size: clamp(18px, 3vw, 30px);
  letter-spacing: 6px;
  color: #ffffff;
  text-transform: uppercase;
}

/* ── ASCII ART ── */
.ascii-art {
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(6px, 1.2vw, 11px);
  line-height: 1.2;
  color: #55aaff;
  white-space: pre;
  text-align: left;
}

/* ── LINKS BOX ── */
.links-box {
  border: 2px solid #ffffff;
  width: clamp(260px, 60vw, 400px);
}

.links-list {
  list-style: none;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.links-list li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.link-arrow {
  color: #ffff55;
  flex-shrink: 0;
  font-size: 16px;
}

.links-list a {
  display: block;
  width: 100%;
  color: #ffffff;
  text-decoration: none;
  letter-spacing: 1px;
  font-size: clamp(14px, 2vw, 16px);
  text-transform: uppercase;
  padding: 10px 8px;
  min-height: 44px;
  display: flex;
  align-items: center;
}

.links-list a:hover,
.links-list a:active {
  background: #ffffff;
  color: #0000aa;
}

/* ── FOOTER BOX ── */
.footer-box {
  border: 2px solid #ffffff;
  width: clamp(260px, 60vw, 400px);
}

.footer-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 14px;
  font-size: clamp(10px, 1.5vw, 12px);
  letter-spacing: 1px;
}

.footer-left  { color: #55aaff; }
.footer-right { color: #ffff55; }

/* ── AUTH FORM ── */
.auth-form {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.auth-field label {
  font-size: 11px;
  letter-spacing: 2px;
  color: #55aaff;
}

.auth-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.auth-input-row input {
  flex: 1;
  background: #000055;
  border: 1px solid #55aaff;
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(13px, 1.5vw, 15px);
  letter-spacing: 1px;
  padding: 8px 10px;
  min-height: 44px;
  outline: none;
}

.auth-input-row input:focus {
  border-color: #ffffff;
  background: #000077;
}

.auth-actions {
  padding-top: 4px;
}

.auth-btn {
  background: none;
  border: 2px solid #ffffff;
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(13px, 1.5vw, 15px);
  letter-spacing: 3px;
  padding: 10px 24px;
  min-height: 44px;
  cursor: pointer;
  text-transform: uppercase;
  width: 100%;
}

.auth-btn:hover,
.auth-btn:active {
  background: #ffffff;
  color: #0000aa;
}

/* ── LEADERBOARD TABLE ── */
.lb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: clamp(12px, 1.5vw, 14px);
  letter-spacing: 1px;
}

.lb-table thead tr {
  background: #55aaff;
  color: #0000aa;
}

.lb-table th {
  padding: 8px 14px;
  text-align: left;
  font-weight: bold;
}

.lb-table td {
  padding: 10px 14px;
  color: #aaddff;
  border-bottom: 1px solid #225588;
}

.lb-table tbody tr:last-child td {
  border-bottom: none;
}

.lb-table tbody tr:hover td {
  background: #000077;
  color: #ffffff;
}

.rank-1 td { color: #ffff55; }
.rank-2 td { color: #aaddff; }
.rank-3 td { color: #ffaa00; }

/* ── AUTH EXTRAS ── */
.auth-error {
  color: #ff5555;
  font-size: clamp(11px, 1.3vw, 13px);
  letter-spacing: 1px;
  border: 1px solid #ff5555;
  padding: 6px 10px;
}

.auth-switch {
  font-size: 11px;
  letter-spacing: 1px;
  color: #55aaff;
}

.auth-switch a {
  color: #ffffff;
  text-decoration: none;
}

.auth-switch a:hover {
  background: #ffffff;
  color: #0000aa;
}

/* ── FOOTER USER / SIGNOUT ── */
.footer-user {
  color: #ffff55;
}

.signout-form {
  display: inline;
}

.signout-btn {
  background: none;
  border: none;
  color: #55aaff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(10px, 1.5vw, 12px);
  letter-spacing: 1px;
  cursor: pointer;
  padding: 0;
}

.signout-btn:hover {
  background: #55aaff;
  color: #0000aa;
}

/* ── LEADERBOARD EXTRAS ── */
.rank-me td { background: #000077; }

.lb-you { color: #55aaff; font-size: 11px; letter-spacing: 1px; }

.lb-empty {
  color: #55aaff;
  padding: 14px;
  letter-spacing: 2px;
}

/* ── BACK LINK ── */
.back-link {
  color: #55aaff;
  text-decoration: none;
  letter-spacing: 1px;
  font-size: clamp(10px, 1.5vw, 12px);
}

.back-link:hover {
  background: #55aaff;
  color: #0000aa;
}

/* ── GAME ── */
.screen:has(.game-wrap) {
  justify-content: flex-start;
  padding: 10px 12px;
  gap: 8px;
}

.screen:has(.game-wrap) .header-box,
.screen:has(.game-wrap) .footer-box {
  width: 100%;
  flex-shrink: 0;
}

.game-wrap {
  border: 2px solid #ffffff;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.game-hud {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 4px 10px;
  font-size: clamp(11px, 2.5vw, 13px);
  letter-spacing: 1px;
  border-bottom: 1px solid #225588;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.hud-score { color: #ffffff; }
.hud-hi    { color: #ffff55; }
.hud-user  { color: #55aaff; margin-left: auto; }

.game-area {
  flex: 1;
  position: relative;
  min-height: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #0000aa;
  touch-action: none;
}

.game-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 100, 0.75);
  pointer-events: none;
}

.overlay-msg {
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(12px, 3vw, 16px);
  letter-spacing: 2px;
  text-align: center;
  white-space: pre;
  border: 2px solid #ffffff;
  padding: 14px 24px;
  line-height: 1.8;
}

.tap-btn {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: transparent;
  border: none;
  color: transparent;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

/* CRT scanline overlay */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0px,
    transparent 3px,
    rgba(0, 0, 0, 0.07) 3px,
    rgba(0, 0, 0, 0.07) 4px
  );
  z-index: 999;
}

/* ── DRAW PAGE ───────────────────────────────────────── */

.screen:has(.draw-wrap) {
  justify-content: flex-start;
  padding: 10px 12px;
  gap: 8px;
}

.screen:has(.draw-wrap) .header-box,
.screen:has(.draw-wrap) .footer-box {
  width: 100%;
  flex-shrink: 0;
}

.draw-wrap {
  border: 2px solid #ffffff;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* ── Draw HUD toolbar ── */
.draw-hud {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 14px;
  padding: 4px 10px;
  border-bottom: 1px solid #225588;
  flex-shrink: 0;
  font-size: clamp(9px, 1.2vw, 11px);
  letter-spacing: 1px;
}

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

.hud-group--right {
  margin-left: auto;
}

.hud-label {
  color: #55aaff;
  font-size: 10px;
  letter-spacing: 2px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Color swatches */
.palette {
  display: flex;
  gap: 3px;
  align-items: center;
}

.palette-swatch {
  width: 18px;
  height: 18px;
  border: 1px solid #225588;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}

.palette-swatch.active {
  border: 2px solid #ffffff;
  outline: 1px solid #55aaff;
}

.palette-swatch:hover {
  border-color: #ffffff;
}

/* Size / tool / action buttons */
.size-btns,
.tool-btns,
.action-btns {
  display: flex;
  gap: 3px;
}

.size-btn,
.tool-btn,
.action-btn {
  background: none;
  border: 1px solid #225588;
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(9px, 1.2vw, 11px);
  letter-spacing: 1px;
  padding: 3px 7px;
  min-height: 24px;
  cursor: pointer;
  text-transform: uppercase;
  white-space: nowrap;
}

.size-btn.active,
.tool-btn.active {
  background: #55aaff;
  color: #0000aa;
  border-color: #55aaff;
}

.size-btn:hover,
.tool-btn:hover,
.action-btn:hover {
  background: #ffffff;
  color: #0000aa;
  border-color: #ffffff;
}

/* Presence */
.presence-list {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.presence-dot {
  font-size: 10px;
  letter-spacing: 1px;
  white-space: nowrap;
}

.presence-dot::before {
  content: '● ';
}

/* ── Draw body (canvas + chat) ── */
.draw-body {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.draw-canvas-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-width: 0;
  background: #0000aa;
}

#drawCanvas,
#lineCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#lineCanvas {
  pointer-events: none;
}

/* ── Chat panel ── */
.draw-chat {
  width: clamp(150px, 18vw, 220px);
  border-left: 1px solid #225588;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-height: 0;
  font-size: clamp(10px, 1.2vw, 12px);
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scrollbar-width: thin;
  scrollbar-color: #225588 #000055;
}

.chat-log::-webkit-scrollbar       { width: 5px; }
.chat-log::-webkit-scrollbar-track { background: #000055; }
.chat-log::-webkit-scrollbar-thumb { background: #225588; }

.chat-msg {
  color: #aaddff;
  word-break: break-word;
  line-height: 1.4;
  letter-spacing: 0.5px;
  font-size: clamp(10px, 1.1vw, 12px);
}

.chat-msg--system {
  color: #ffff55;
  font-size: 0.9em;
}

.chat-user {
  color: #55aaff;
}

.chat-input-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  border-top: 1px solid #225588;
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  background: #000055;
  border: 1px solid #225588;
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(10px, 1.1vw, 12px);
  letter-spacing: 1px;
  padding: 5px 6px;
  outline: none;
  text-transform: uppercase;
  min-width: 0;
}

.chat-input:focus {
  border-color: #ffffff;
  background: #000077;
}

/* ── Mobile chat FAB + overlay ── */
.chat-fab {
  display: none; /* shown via media query */
  position: absolute;
  bottom: 14px;
  right: 14px;
  z-index: 10;
  background: #0000aa;
  border: 2px solid #ffffff;
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  letter-spacing: 2px;
  padding: 8px 14px;
  cursor: pointer;
  text-transform: uppercase;
  white-space: nowrap;
}

.chat-fab:hover {
  background: #ffffff;
  color: #0000aa;
}

.chat-fab--hidden {
  display: none !important;
}

.chat-fab-badge {
  display: none;
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff5555;
  color: #ffffff;
  font-size: 10px;
  letter-spacing: 0;
  min-width: 18px;
  height: 18px;
  border-radius: 0;
  border: 1px solid #ffffff;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

.chat-close-row {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 4px;
  border-bottom: 1px solid #225588;
  flex-shrink: 0;
}

.chat-close-btn {
  background: none;
  border: 1px solid #225588;
  color: #55aaff;
  font-family: 'Courier New', Courier, monospace;
  font-size: 10px;
  letter-spacing: 1px;
  padding: 3px 8px;
  cursor: pointer;
  text-transform: uppercase;
}

.chat-close-btn:hover {
  background: #ffffff;
  color: #0000aa;
  border-color: #ffffff;
}

@media (max-width: 600px) {
  .draw-chat {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 20;
    width: 100%;
    background: rgba(0, 0, 100, 0.97);
    border-left: none;
    border: 2px solid #ffffff;
  }

  .draw-chat.chat-overlay-open {
    display: flex;
  }

  .chat-close-row {
    display: flex;
  }

  .chat-fab {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .draw-canvas-wrap {
    position: relative;
  }
}

/* ── STANDALONE CHAT PAGE ────────────────────────────── */

.screen:has(.chat-wrap) {
  justify-content: flex-start;
  padding: 10px 12px;
  gap: 8px;
}

.screen:has(.chat-wrap) .header-box,
.screen:has(.chat-wrap) .footer-box {
  width: 100%;
  flex-shrink: 0;
}

.chat-wrap {
  border: 2px solid #ffffff;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.chat-hud {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px;
  border-bottom: 1px solid #225588;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.chat-page-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-input-row--page {
  padding: 6px 10px;
}

.chat-input-row--page .chat-input {
  font-size: clamp(12px, 1.5vw, 14px);
  padding: 8px 10px;
  min-height: 40px;
}

/* ── HOME CHAT WIDGET ────────────────────────────────── */

.home-chat-widget {
  position: fixed;
  bottom: 0;
  right: 24px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.home-chat-tab {
  position: relative;
  background: #0000aa;
  border: 2px solid #ffffff;
  border-bottom: none;
  color: #ffffff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(10px, 1.3vw, 12px);
  letter-spacing: 2px;
  padding: 7px 16px;
  cursor: pointer;
  text-transform: uppercase;
  white-space: nowrap;
}

.home-chat-tab:hover,
.home-chat-tab--active {
  background: #ffffff;
  color: #0000aa;
}

.home-chat-tab--active .chat-fab-badge {
  display: none !important;
}

.home-chat-panel {
  display: none;
  flex-direction: column;
  width: clamp(260px, 30vw, 360px);
  height: clamp(280px, 40vh, 420px);
  background: #0000aa;
  border: 2px solid #ffffff;
  border-bottom: none;
}

.home-chat-panel--open {
  display: flex;
}

.home-chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid #225588;
  flex-shrink: 0;
}

.home-chat-header .chat-close-btn {
  margin-left: auto;
  flex-shrink: 0;
}

.presence-list--sm .presence-dot {
  font-size: 9px;
}

/* Badge shared by draw FAB and home tab */
.home-chat-tab .chat-fab-badge {
  display: none;
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff5555;
  color: #ffffff;
  font-size: 10px;
  letter-spacing: 0;
  min-width: 18px;
  height: 18px;
  border: 1px solid #ffffff;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

/* ── PWA Install Button ── */
.pwa-install-btn {
  background: none;
  border: 1px solid #55aaff;
  color: #55aaff;
  font-family: 'Courier New', Courier, monospace;
  font-size: 11px;
  letter-spacing: 1px;
  padding: 3px 8px;
  cursor: pointer;
  margin-left: 12px;
  vertical-align: middle;
}
.pwa-install-btn:hover {
  background: #55aaff;
  color: #0000aa;
}

.chat-fullpage-link {
  color: #55aaff;
  text-decoration: none;
  font-size: 10px;
  letter-spacing: 1px;
  white-space: nowrap;
  flex-shrink: 0;
}

.chat-fullpage-link:hover {
  background: #55aaff;
  color: #0000aa;
}

/* ── CHAT FEATURES (shared by page + widget) ─────────── */

/* Message structure */
.chat-msg-wrap {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 3px 0;
  border-bottom: 1px solid rgba(34, 85, 136, 0.3);
}

.chat-msg-wrap:last-child {
  border-bottom: none;
}

.chat-msg-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px;
  line-height: 1.4;
}

.chat-msg-time {
  color: #3388cc;
  font-size: 10px;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}

.chat-msg-body {
  color: #aaddff;
  word-break: break-word;
}

/* Attachment display */
.chat-attach-preview {
  margin-top: 4px;
}

.chat-img {
  display: block;
  max-width: min(200px, 100%);
  max-height: 150px;
  border: 1px solid #225588;
  cursor: pointer;
  object-fit: cover;
}

.chat-img:hover {
  border-color: #ffffff;
}

.chat-video {
  display: block;
  max-width: min(260px, 100%);
  border: 1px solid #225588;
  background: #000000;
}

.chat-attach-link {
  color: #55aaff;
  text-decoration: none;
  font-size: clamp(10px, 1.1vw, 12px);
  letter-spacing: 1px;
  word-break: break-all;
}

.chat-attach-link:hover {
  background: #55aaff;
  color: #0000aa;
}

/* Typing indicator */
.chat-typing-bar {
  color: #ffff55;
  font-size: 10px;
  letter-spacing: 1px;
  padding: 2px 8px;
  min-height: 18px;
  flex-shrink: 0;
  font-family: 'Courier New', Courier, monospace;
}

/* Pending attachment bar */
.chat-pending-attach {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-top: 1px solid #225588;
  background: #000055;
  flex-shrink: 0;
}

.chat-pending-label {
  color: #55aaff;
  font-family: 'Courier New', Courier, monospace;
  font-size: clamp(9px, 1.1vw, 11px);
  letter-spacing: 1px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-pending-cancel {
  background: none;
  border: 1px solid #ff5555;
  color: #ff5555;
  font-family: 'Courier New', Courier, monospace;
  font-size: 10px;
  letter-spacing: 1px;
  padding: 2px 6px;
  cursor: pointer;
  flex-shrink: 0;
}

.chat-pending-cancel:hover {
  background: #ff5555;
  color: #ffffff;
}

/* Attach button */
.attach-btn {
  background: none;
  border: 1px solid #225588;
  color: #55aaff;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  padding: 3px 8px;
  min-height: 34px;
  cursor: pointer;
  flex-shrink: 0;
}

.attach-btn:hover {
  background: #55aaff;
  color: #0000aa;
  border-color: #55aaff;
}

.attach-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Reactions */
.chat-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  margin-top: 2px;
}

.chat-reaction-btn {
  background: none;
  border: 1px solid #225588;
  color: #aaddff;
  font-family: 'Courier New', Courier, monospace;
  font-size: 11px;
  padding: 1px 6px;
  cursor: pointer;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.chat-reaction-btn:hover {
  border-color: #ffff55;
  color: #ffff55;
}

.chat-reaction-add {
  background: none;
  border: 1px dashed #225588;
  color: #3388cc;
  font-family: 'Courier New', Courier, monospace;
  font-size: 9px;
  padding: 1px 5px;
  cursor: pointer;
  letter-spacing: 1px;
}

.chat-reaction-add:hover {
  border-color: #55aaff;
  color: #55aaff;
}

/* Emoji picker popover */
.emoji-picker {
  display: flex;
  gap: 4px;
  background: #000055;
  border: 1px solid #225588;
  padding: 4px 6px;
  position: absolute;
  z-index: 50;
}

.emoji-option {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 2px;
  line-height: 1;
}

.emoji-option:hover {
  background: #225588;
}

/* Make reaction bar wrapper position:relative for the popover */
.chat-reactions {
  position: relative;
}

/* Standalone chat page input row enlargement */
.chat-input-row--page .chat-input {
  font-size: clamp(12px, 1.5vw, 14px);
  padding: 8px 10px;
  min-height: 40px;
}

