:root {
  --ui-bg:      rgba(0,0,0,0.55);
  --ui-bg-soft: rgba(0,0,0,0.42);
  --ui-border:  rgba(255,255,255,0.15);
  --ui-border-hi: rgba(255,255,255,0.4);
  --ui-accent:  #ffd47a;
  --ui-accent-green: #7dff7d;
  --ui-accent-orange: #ffb060;
  --ui-radius:  4px;
  --ui-font:    monospace;
  --ui-font-sm: 12px;
  --ui-font-md: 13px;
  --ui-font-lg: 15px;
  --panel-left-w:  clamp(180px, 30vw, 320px);
  --panel-right-w: clamp(120px, 18vw, 200px);
  --craft-btn-size: 48px;
  --craft-panel-w:  calc(var(--craft-btn-size) + 8px);
}

html, body {
  margin: 0; padding: 0;
  overflow: hidden;
  background: #1a1510;
  height: 100%;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}
canvas.assign-mode { cursor: crosshair; }

/* ===================== HUD ===================== */
#hud {
  position: absolute;
  top: max(10px, env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-family: var(--ui-font);
  background: var(--ui-bg);
  padding: 6px 10px;
  border-radius: var(--ui-radius);
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  max-width: min(90vw, 640px);
  justify-content: center;
  font-size: var(--ui-font-md);
  z-index: 10;
}

/* ===================== Left panel ===================== */
#leftPanel {
  position: absolute;
  left: max(10px, env(safe-area-inset-left, 0px));
  top:    max(10px, env(safe-area-inset-top, 0px));
  bottom: max(10px, env(safe-area-inset-bottom, 0px));
  width: var(--panel-left-w);
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  z-index: 5;
}

/* Чат — блокирует тач только когда input в фокусе (переключается из JS) */
#chatLog {
  background: var(--ui-bg-soft);
  border-radius: var(--ui-radius);
  padding: 6px 8px;
  font-family: var(--ui-font);
  font-size: var(--ui-font-sm);
  color: #fff;
  flex: 0 0 33%;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  pointer-events: none;
  box-sizing: border-box;
  word-wrap: break-word;
  touch-action: pan-y;
}
#chatLog::-webkit-scrollbar { display: none; }

#chatBar {
  display: flex;
  gap: 4px;
  align-items: center;
  margin-bottom: 6px;
}

#chatScopeBtn {
  background: rgba(80,140,255,0.4);
  border: 1px solid rgba(120,180,255,0.6);
  border-radius: 3px;
  color: #fff;
  padding: 2px 6px;
  cursor: pointer;
  font-family: var(--ui-font);
  font-size: 11px;
  user-select: none;
  pointer-events: auto;
}
#chatScopeBtn:hover { background: rgba(80,140,255,0.6); }
#chatScopeBtn.club { background: rgba(120,220,120,0.4); border-color: rgba(160,255,160,0.6); }

#chatInlineInput {
  flex: 1;
  background: #1a1510;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 3px;
  color: #fff;
  font-family: var(--ui-font);
  font-size: var(--ui-font-sm);
  padding: 3px 5px;
  outline: none;
  pointer-events: auto;
}
#chatInlineInput:focus { border-color: rgba(120,180,255,0.7); }

#chatMessages { pointer-events: none; }

.chat-msg { line-height: 1.35; margin-bottom: 2px; }
.chat-msg .chat-club { color: #fff; opacity: 0.7; }
.chat-msg .chat-nick { color: #fff; font-weight: bold; }
.chat-msg .chat-scope {
  color: #9dff9d; font-size: 10px; opacity: 0.6; margin-right: 3px;
}

/* ===================== Craft panel ===================== */
/* Узкая колонка: тач блокируется только над кнопками, остальное свободно */
#craftPanel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 auto;
  min-height: 0;
  width: var(--craft-panel-w);
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
  user-select: none;
  touch-action: pan-y;
  pointer-events: auto;
}
#craftPanel::-webkit-scrollbar { display: none; }

.craft-btn {
  position: relative;
  background-color: #3a2a1a;
  background-image: repeating-conic-gradient(#3a2a1a 0 25%, #7a5a3a 0 50%);
  background-size: 8px 8px;
  border: 2px solid var(--ui-border);
  border-radius: 6px;
  width: var(--craft-btn-size);
  height: var(--craft-btn-size);
  box-sizing: border-box;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: border-color 0.15s, opacity 0.15s, box-shadow 0.15s;
  flex: 0 0 auto;
  box-shadow: 0 0 0 2px rgba(0,0,0,0.35) inset;
  pointer-events: auto;
}
.craft-btn:hover { border-color: var(--ui-border-hi); }
.craft-btn.disabled { opacity: 0.3; cursor: not-allowed; }
.craft-btn > canvas { display: block; }
.craft-btn.ready {
  border-color: var(--ui-accent-green);
  box-shadow: 0 0 10px rgba(120,255,120,0.7);
}

.craft-badge {
  position: absolute;
  top: -6px; right: -6px;
  background: #40c040;
  color: #fff;
  font-family: var(--ui-font);
  font-size: 10px;
  font-weight: bold;
  border-radius: 9px;
  min-width: 16px;
  height: 16px;
  line-height: 16px;
  text-align: center;
  padding: 0 4px;
  pointer-events: none;
  box-sizing: border-box;
}

/* ===================== Right panel ===================== */
#rightPanel {
  position: absolute;
  /* миникарта: 16 margin + 128 (64 * 2) + 16 gap = 160 */
  top: max(160px, calc(env(safe-area-inset-top, 0px) + 150px));
  right: max(16px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  font-family: var(--ui-font);
  font-size: var(--ui-font-sm);
  color: #fff;
  user-select: none;
  pointer-events: none;
  width: var(--panel-right-w);
  z-index: 5;
}

@media (max-width: 500px) {
  #rightPanel { display: none; }
}

#playerList {
  display: flex; flex-direction: column; gap: 3px;
  pointer-events: none;
  width: 100%;
  box-sizing: border-box;
  white-space: nowrap;
}

.player-row {
  display: flex; align-items: center; gap: 6px;
  background: rgba(0,0,0,0.4);
  border-radius: 3px;
  padding: 2px 6px;
  opacity: 0.7;
  cursor: pointer;
  transition: opacity 0.1s, background 0.1s;
  pointer-events: auto;
}
.player-row:hover { opacity: 1; background: rgba(0,0,0,0.6); }
.player-row.me {
  opacity: 1;
  font-weight: bold;
  background: rgba(255,255,255,0.15);
}
.player-row.selected { opacity: 1; outline: 1px solid #fff; }
.player-row > span {
  font-family: var(--ui-font);
  letter-spacing: 0;
  white-space: pre;
  color: #fff;
}
.player-row .dot {
  width: 8px; height: 8px;
  display: block;
  flex: 0 0 auto;
  image-rendering: pixelated;
}

/* ===================== Club panel ===================== */
#clubPanel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--ui-bg-soft);
  border-radius: var(--ui-radius);
  padding: 8px;
  width: 100%;
  box-sizing: border-box;
  pointer-events: none;
}
#clubPanel:empty { display: none; }
#clubPanel .club-btn,
#clubPanel input { pointer-events: auto; }

.club-create {
  display: flex; flex-direction: column; gap: 4px; font-size: var(--ui-font-sm);
}
.club-info {
  display: flex; flex-direction: row; align-items: center;
  gap: 4px; font-size: var(--ui-font-sm);
}
.club-info span { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.club-info b { color: #9dff9d; }

.club-invite, .club-request {
  display: flex; flex-direction: row; align-items: center; justify-content: space-between;
  gap: 4px; font-size: 11px;
  background: rgba(255,255,255,0.07);
  border-radius: 3px;
  padding: 3px 5px;
}
.club-invite span, .club-request span { flex: 1; overflow: hidden; text-overflow: ellipsis; }

.club-btn {
  background: rgba(80,140,255,0.4);
  border: 1px solid rgba(120,180,255,0.6);
  border-radius: 3px;
  color: #fff;
  padding: 4px 8px;
  cursor: pointer;
  font-family: var(--ui-font);
  font-size: var(--ui-font-sm);
  transition: background 0.15s;
}
.club-btn:hover { background: rgba(80,140,255,0.6); }
.club-btn.small { padding: 1px 5px; font-size: 11px; }
.club-btn.danger {
  background: rgba(200,60,60,0.4);
  border-color: rgba(255,120,120,0.7);
}
.club-btn.danger:hover { background: rgba(200,60,60,0.65); }

#clubNameInput {
  background: #1a1510;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 3px;
  color: #fff;
  font-family: var(--ui-font);
  font-size: var(--ui-font-sm);
  padding: 4px 6px;
  width: 100%;
  box-sizing: border-box;
  outline: none;
}
#clubNameInput:focus { border-color: rgba(255,255,255,0.45); }

/* ===================== Build bar ===================== */
#buildBar {
  position: absolute;
  bottom: max(10px, env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  padding: 6px;
  background: var(--ui-bg-soft);
  border-radius: 6px;
  pointer-events: none;
  max-width: 90vw;
  overflow-x: auto;
  touch-action: pan-x;
  scrollbar-width: none;
  z-index: 5;
}
#buildBar::-webkit-scrollbar { display: none; }
#buildBar:empty { display: none; }

.build-btn {
  background-color: #3a2a1a;
  background-image: repeating-conic-gradient(#3a2a1a 0 25%, #7a5a3a 0 50%);
  background-size: 8px 8px;
  border: 2px solid var(--ui-border);
  border-radius: 6px;
  width: 48px;
  height: 48px;
  box-sizing: border-box;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: border-color 0.15s, opacity 0.15s;
  box-shadow: 0 0 0 2px rgba(0,0,0,0.35) inset;
  pointer-events: auto;
  flex: 0 0 auto;
}
.build-btn:hover { border-color: var(--ui-border-hi); }
.build-btn.active {
  border-color: var(--ui-accent-green);
  box-shadow: 0 0 8px rgba(120,255,120,0.6);
}
.build-btn > canvas { display: block; }

/* ===================== Deconstruct button ===================== */
#deconstructBtn {
  background: var(--ui-bg);
  border: 2px solid var(--ui-border);
  border-radius: var(--ui-radius);
  padding: 2px 8px;
  color: #fff;
  font-family: var(--ui-font);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  pointer-events: auto;
  transition: background 0.15s, border-color 0.15s;
  margin-left: 4px;
}
#deconstructBtn:hover { border-color: var(--ui-border-hi); }
#deconstructBtn.active {
  background: rgba(255,140,40,0.6);
  border-color: var(--ui-accent-orange);
}

/* ===================== Join overlay ===================== */
#joinOverlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  font-family: var(--ui-font);
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
           env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}
#joinOverlay.hidden { display: none; }

.join-box {
  background: #221c15;
  border: 2px solid var(--ui-border);
  border-radius: 8px;
  padding: 24px 28px;
  min-width: 260px;
  max-width: 90vw;
  display: flex; flex-direction: column; gap: 12px;
  align-items: stretch;
}
.join-box h2 {
  margin: 0 0 8px 0;
  color: #fff;
  font-size: 20px;
  text-align: center;
  letter-spacing: 2px;
}
#nickInput {
  background: #1a1510;
  border: 2px solid var(--ui-border);
  border-radius: var(--ui-radius);
  padding: 8px 10px;
  color: #fff;
  font-family: var(--ui-font);
  font-size: 16px;
  outline: none;
}
#nickInput:focus { border-color: rgba(255,255,255,0.45); }
#joinBtn {
  background: rgba(255,140,40,0.35);
  border: 2px solid var(--ui-accent-orange);
  border-radius: var(--ui-radius);
  padding: 8px 10px;
  color: #fff;
  font-family: var(--ui-font);
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s;
}
#joinBtn:hover { background: rgba(255,140,40,0.6); }
#joinError {
  color: #ff8080;
  font-size: 13px;
  min-height: 16px;
  text-align: center;
}

/* ===================== Tooltip ===================== */
#tooltip {
  position: fixed;
  z-index: 500;
  background: rgba(0,0,0,0.9);
  color: #fff;
  font-family: var(--ui-font);
  font-size: var(--ui-font-sm);
  padding: 8px 10px;
  border-radius: var(--ui-radius);
  border: 1px solid var(--ui-border);
  pointer-events: none;
  display: none;
  max-width: min(280px, 80vw);
  line-height: 1.4;
  white-space: pre-line;
}
#tooltip.visible { display: block; }
#tooltip .tt-title { color: #9dff9d; font-weight: bold; margin-bottom: 4px; }
#tooltip .tt-row { display: flex; justify-content: space-between; gap: 10px; }
#tooltip .tt-row span:last-child { color: var(--ui-accent); }
#tooltip .tt-sep {
  height: 1px; background: var(--ui-border); margin: 4px 0;
}

/* ===================== Small screens ===================== */
@media (max-width: 700px) {
  :root {
    --ui-font-sm: 11px;
    --ui-font-md: 12px;
    --ui-font-lg: 13px;
    --panel-left-w: clamp(160px, 45vw, 260px);
    --panel-right-w: clamp(110px, 30vw, 160px);
    --craft-btn-size: 44px;
  }
  #hud { gap: 6px; padding: 4px 8px; }
}