:root {
  --bg: #0f1419;
  --panel: #1a2332;
  --border: #2d3a4f;
  --text: #e7ecf3;
  --muted: #8b9cb3;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --radius: 8px;
  --tap: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

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

html {
  height: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding:
    var(--safe-top)
    var(--safe-right)
    var(--safe-bottom)
    var(--safe-left);
  overscroll-behavior: none;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.85rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  gap: 0.5rem;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.topbar-left {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  min-width: 0;
  flex: 1;
}

.progress {
  color: var(--muted);
  font-size: 0.78rem;
  word-break: break-all;
}

.topbar-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.btn {
  border: none;
  border-radius: var(--radius);
  padding: 0.55rem 0.85rem;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 500;
  min-height: var(--tap);
  touch-action: manipulation;
}

.btn.primary {
  background: var(--accent);
  color: #fff;
}

.btn.primary:active {
  background: var(--accent-hover);
}

.btn.secondary {
  background: var(--border);
  color: var(--text);
}

.btn.secondary:active {
  background: #3d4f6a;
}

.btn.small {
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  min-height: var(--tap);
}

.btn.danger {
  background: #7f1d1d;
  color: #fecaca;
}

.btn.danger:active {
  background: var(--danger);
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr min(300px, 32vw);
  min-height: 0;
}

.canvas-panel {
  display: flex;
  flex-direction: column;
  padding: 0.65rem;
  min-height: 0;
  overflow: hidden;
}

.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #080b10;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: auto;
  min-height: 240px;
  -webkit-overflow-scrolling: touch;
}

#canvas {
  display: block;
  max-width: 100%;
  max-height: min(72dvh, calc(100dvh - 220px));
  cursor: crosshair;
  touch-action: none;
}

.status {
  margin: 0.5rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.35;
}

.sidebar {
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 0.75rem;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.sidebar h2 {
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
}

.sidebar h3 {
  margin: 0.85rem 0 0.35rem;
  font-size: 0.88rem;
}

.hint {
  font-size: 0.72rem;
  color: var(--muted);
  margin: 0 0 0.6rem;
  line-height: 1.35;
}

.tag-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tag-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0.7rem;
  min-height: var(--tap);
  border-radius: var(--radius);
  cursor: pointer;
  margin-bottom: 0.3rem;
  border: 1px solid transparent;
  touch-action: manipulation;
}

.tag-item:active {
  background: rgba(255, 255, 255, 0.06);
}

.tag-item.active {
  background: rgba(59, 130, 246, 0.15);
  border-color: var(--accent);
}

.tag-swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.tag-name {
  flex: 1;
  font-size: 0.88rem;
  line-height: 1.25;
  word-break: break-word;
}

.tag-count {
  font-size: 0.78rem;
  color: var(--muted);
  flex-shrink: 0;
}

.row {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.35rem;
  align-items: stretch;
}

.row input {
  flex: 1;
  min-width: 0;
}

input[type="text"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.55rem 0.6rem;
  font-size: 16px;
  min-height: var(--tap);
}

label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.65rem;
}

.add-tag {
  margin-top: 0.5rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

.tag-actions .btn,
.point-actions .btn {
  width: 100%;
  margin-top: 0.4rem;
}

.done-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
}

.done-screen h2 {
  margin: 0 0 0.5rem;
}

/* Tablet: sidebar below canvas */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .sidebar {
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: min(42dvh, 360px);
  }

  #canvas {
    max-height: min(58dvh, calc(100dvh - 320px));
  }

  .canvas-wrap {
    min-height: min(50dvh, 400px);
  }
}

/* Phone */
@media (max-width: 600px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .topbar-actions .btn.primary {
    grid-column: 1 / -1;
  }

  .topbar-actions .btn {
    width: 100%;
    padding-left: 0.35rem;
    padding-right: 0.35rem;
    font-size: 0.85rem;
  }

  .canvas-panel {
    padding: 0.45rem;
  }

  .sidebar {
    max-height: min(45dvh, 380px);
    padding: 0.65rem;
  }

  .tag-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.25rem;
  }

  .tag-item {
    margin-bottom: 0;
  }

  .tag-actions,
  .point-actions {
    display: grid;
    gap: 0.35rem;
  }
}

@media (pointer: coarse) {
  .tag-item {
    padding: 0.75rem 0.7rem;
  }

  #canvas {
    max-height: min(62dvh, calc(100dvh - 280px));
  }
}
