:root {
  color-scheme: light;
  --ink: #25302a;
  --muted: rgba(37, 48, 42, 0.68);
  --paper: rgba(255, 252, 240, 0.82);
  --paper-strong: rgba(255, 252, 240, 0.94);
  --line: rgba(95, 112, 92, 0.22);
  --accent: #227d6d;
  --accent-strong: #0f5d52;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: #f7f0dc;
}

button,
input {
  font: inherit;
}

[hidden] {
  display: none !important;
}

#worldCanvas {
  display: block;
  width: 100vw;
  height: 100vh;
  touch-action: none;
}

.start-panel {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(rgba(255, 252, 240, 0.62), rgba(247, 240, 220, 0.86)),
    url("img/tiles/grass.png");
  background-size: auto, 128px 128px;
  z-index: 20;
}

.start-inner {
  width: min(440px, 100%);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-strong);
  box-shadow: 0 20px 60px rgba(63, 53, 34, 0.18);
}

.start-inner h1 {
  margin: 0 0 20px;
  font-size: 32px;
  line-height: 1.05;
  letter-spacing: 0;
}

.join-form label,
.coords-grid label {
  display: grid;
  gap: 6px;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  text-transform: uppercase;
}

.join-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

input {
  min-width: 0;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 12px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  outline: none;
}

input:focus {
  border-color: rgba(34, 125, 109, 0.72);
  box-shadow: 0 0 0 3px rgba(34, 125, 109, 0.12);
}

button {
  height: 42px;
  border: 1px solid rgba(34, 125, 109, 0.38);
  border-radius: 6px;
  padding: 0 14px;
  font-weight: 800;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
}

button:hover {
  background: var(--accent-strong);
}

.hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.topbar,
.top-left,
.top-actions,
.palette-panel,
.tile-panel,
.tile-bar,
.recent-bar,
.coords-panel,
.leaderboard-panel,
.chat-form,
.bottom-chat-actions,
.world-chat-panel,
.connection-overlay,
.joystick {
  pointer-events: auto;
}

.topbar {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  z-index: 8;
}

.top-center {
  grid-column: 2;
  display: flex;
  gap: 8px;
  justify-content: center;
}

.top-left {
  grid-column: 1;
  justify-self: start;
}

.top-actions {
  grid-column: 3;
  justify-self: end;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.coordinate-pill,
.fps-pill,
.icon-button,
.coords-panel,
.leaderboard-panel,
.chat-form input,
.chat-form button,
.bottom-chat-actions button,
.world-chat-panel,
.connection-panel,
.palette-panel {
  border: 1px solid var(--line);
  background: var(--paper);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 28px rgba(63, 53, 34, 0.12);
}

.coordinate-pill,
.fps-pill {
  min-width: 118px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  padding: 0 12px;
  font-size: 12px;
  font-weight: 900;
  color: var(--muted);
}

.coordinate-pill {
  min-width: 138px;
  justify-content: center;
  color: var(--ink);
  cursor: pointer;
}

.fps-pill {
  min-width: 72px;
  color: var(--ink);
}

.icon-button {
  height: 34px;
  color: var(--ink);
}

.icon-button[aria-pressed="true"] {
  color: #fff;
  background: #b85430;
}

.palette-panel {
  position: fixed;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(calc(100vw - 24px), 272px);
  display: grid;
  gap: 6px;
  padding: 8px;
  border-radius: 8px;
  z-index: 8;
}

.recent-bar {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 44px;
  gap: 7px;
  overflow-x: auto;
  min-height: 44px;
}

.tile-panel {
  position: fixed;
  top: max(58px, calc(env(safe-area-inset-top) + 58px));
  left: max(12px, env(safe-area-inset-left));
  width: 78px;
  max-height: min(520px, calc(100vh - 136px));
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 28px rgba(63, 53, 34, 0.12);
  z-index: 8;
}

.tile-bar {
  display: grid;
  grid-auto-flow: row;
  grid-auto-rows: 44px;
  gap: 7px;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 6px;
  max-height: calc(min(520px, 100vh - 136px) - 16px);
}

.recent-bar:empty {
  display: none;
}

.tile-button {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.86);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.64);
}

.tile-button.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.86), 0 0 0 3px rgba(34, 125, 109, 0.28);
}

.tile-button img {
  width: 32px;
  height: 32px;
  image-rendering: pixelated;
  vertical-align: middle;
}

.custom-color-tile {
  position: relative;
}

.custom-color-tile::after {
  content: "";
  position: absolute;
  inset: 8px;
  border: 1px solid rgba(255, 255, 255, 0.86);
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.32);
}

.eyedropper-button[aria-pressed="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.86), 0 0 0 3px rgba(34, 125, 109, 0.28);
}

.eyedropper-icon {
  position: relative;
  display: inline-block;
  width: 22px;
  height: 22px;
  transform: rotate(-45deg) translateY(-1px);
}

.eyedropper-icon::before {
  content: "";
  position: absolute;
  left: 8px;
  top: 1px;
  width: 6px;
  height: 13px;
  border: 2px solid #050505;
  border-radius: 2px;
  background: #fff;
}

.eyedropper-icon::after {
  content: "";
  position: absolute;
  left: 10px;
  top: 13px;
  width: 3px;
  height: 8px;
  background: #050505;
  border-radius: 0 0 2px 2px;
}

.native-color-input {
  position: fixed;
  left: max(28px, calc(env(safe-area-inset-left) + 28px));
  top: max(76px, calc(env(safe-area-inset-top) + 76px));
  width: 44px;
  height: 44px;
  opacity: 0.001;
  pointer-events: none;
  z-index: -1;
}

.chat-form {
  position: fixed;
  left: 50%;
  bottom: max(82px, calc(env(safe-area-inset-bottom) + 82px));
  width: min(460px, calc(100vw - 24px));
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  z-index: 9;
}

.bottom-chat-actions {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
  z-index: 8;
}

.bottom-chat-actions button {
  position: relative;
  height: 42px;
  color: var(--ink);
}

.chat-unread-badge {
  position: absolute;
  top: -7px;
  right: -7px;
  min-width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.9);
  border-radius: 999px;
  padding: 0 5px;
  color: #fff;
  background: #d92525;
  font-size: 11px;
  line-height: 1;
  font-weight: 900;
  box-shadow: 0 4px 10px rgba(90, 20, 20, 0.24);
}

.world-chat-panel {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  top: max(62px, calc(env(safe-area-inset-top) + 62px));
  bottom: max(68px, calc(env(safe-area-inset-bottom) + 68px));
  width: min(360px, calc(100vw - 24px));
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  z-index: 9;
}

.world-chat-messages {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  line-height: 1.25;
}

.world-chat-message {
  padding: 7px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.62);
}

.world-chat-name {
  font-weight: 900;
  color: var(--accent-strong);
}

.coord-chat-link {
  display: inline;
  height: auto;
  min-height: 0;
  border: 0;
  border-radius: 0;
  padding: 0;
  color: #166ee8;
  background: transparent;
  box-shadow: none;
  backdrop-filter: none;
  font-weight: 900;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.coord-chat-link:hover {
  color: #0d56bd;
  background: transparent;
}

.world-chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.world-chat-form input {
  width: 100%;
}

.world-chat-form button {
  color: var(--ink);
}

.chat-form input {
  width: 100%;
}

.chat-form button {
  color: var(--ink);
}

.coords-panel {
  position: fixed;
  top: 58px;
  right: max(12px, env(safe-area-inset-right));
  width: min(330px, calc(100vw - 24px));
  display: grid;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-strong);
  box-shadow: 0 16px 46px rgba(63, 53, 34, 0.18);
  z-index: 9;
}

.leaderboard-panel {
  position: fixed;
  top: 58px;
  right: max(12px, env(safe-area-inset-right));
  width: min(310px, calc(100vw - 24px));
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 8px;
  z-index: 9;
}

.panel-title {
  font-size: 12px;
  font-weight: 900;
  color: var(--muted);
  text-transform: uppercase;
}

.leaderboard-rows {
  display: grid;
  gap: 6px;
}

.leaderboard-row {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 8px;
  align-items: center;
  min-height: 34px;
  padding: 6px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.62);
}

.leaderboard-message {
  display: block;
  padding: 10px 12px;
  min-height: auto;
}

.leaderboard-swatch {
  width: 24px;
  height: 24px;
  border-radius: 5px;
  border: 1px solid rgba(37, 48, 42, 0.2);
  image-rendering: pixelated;
  background-size: cover;
}

.leaderboard-count {
  font-weight: 900;
}

.connection-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  border: 0;
  background: rgba(37, 48, 42, 0.32);
  box-shadow: none;
  z-index: 30;
}

.connection-panel {
  width: min(360px, 100%);
  padding: 18px;
  border-radius: 8px;
}

.connection-panel h2 {
  margin: 0 0 8px;
  font-size: 24px;
}

.connection-panel p {
  margin: 0 0 14px;
  color: var(--muted);
}

.coords-tabs,
.coords-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.tab-button {
  color: var(--ink);
  background: rgba(255, 255, 255, 0.58);
}

.tab-button.active {
  color: #fff;
  background: var(--accent);
}

.coords-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.coords-grid input {
  width: 100%;
}

.joystick {
  position: fixed;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(98px, calc(env(safe-area-inset-bottom) + 98px));
  width: 116px;
  height: 116px;
  display: none;
  border-radius: 50%;
  border: 1px solid rgba(37, 48, 42, 0.18);
  background: rgba(255, 252, 240, 0.48);
  backdrop-filter: blur(8px);
  touch-action: none;
  z-index: 1;
}

.joystick-knob {
  position: absolute;
  left: 38px;
  top: 38px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(34, 125, 109, 0.82);
  box-shadow: 0 8px 20px rgba(37, 48, 42, 0.16);
}

@media (hover: none), (pointer: coarse) {
  .joystick {
    display: block;
  }

  .palette-panel {
    width: max-content;
    max-width: calc(100vw - 24px);
  }
}

@media (max-width: 620px) {
  .topbar {
    grid-template-columns: auto 1fr auto;
    gap: 8px;
  }

  .top-left {
    grid-column: 1;
    grid-row: 1;
    justify-self: start;
  }

  .top-center {
    grid-column: 2;
    grid-row: 1;
    justify-self: center;
  }

  .top-actions {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: center;
  }

  .top-actions {
    max-width: calc(100vw - 24px);
    justify-content: center;
  }

  .coords-panel,
  .leaderboard-panel {
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    width: auto;
    top: 102px;
  }

  .palette-panel {
    left: auto;
    right: max(12px, env(safe-area-inset-right));
    bottom: max(14px, env(safe-area-inset-bottom));
    width: max-content;
    max-width: calc(100vw - 142px);
    transform: none;
    padding: 6px;
  }

  .palette-panel .recent-bar {
    grid-auto-columns: 32px;
    gap: 4px;
    min-height: 32px;
    overflow-x: hidden;
  }

  .palette-panel .tile-button {
    width: 32px;
    height: 32px;
  }

  .palette-panel .tile-button img {
    width: 26px;
    height: 26px;
  }

  .bottom-chat-actions button {
    height: 36px;
    padding: 0 8px;
    font-size: 12px;
  }

  .chat-form {
    bottom: max(112px, calc(env(safe-area-inset-bottom) + 112px));
  }

  .world-chat-panel {
    right: max(12px, env(safe-area-inset-right));
    width: auto;
    top: 118px;
  }

  .tile-panel {
    top: 102px;
    max-height: calc(100vh - 196px);
  }

  .tile-bar {
    max-height: calc(100vh - 212px);
  }
}
