:root {
  --bg: #0f172a;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-soft: #eef2ff;
  --danger: #e11d48;
  --danger-soft: #fff1f2;
  --success: #10b981;
  --warning: #f59e0b;
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.08);
  --shadow-md: 0 10px 24px -12px rgba(15, 23, 42, 0.35);
  --shadow-lg: 0 30px 60px -25px rgba(15, 23, 42, 0.45);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial,
    "Noto Sans", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 12% -10%, #e0e7ff 0%, rgba(224, 231, 255, 0) 60%),
    radial-gradient(900px 500px at 100% 0%, #ccfbf1 0%, rgba(204, 251, 241, 0) 55%),
    linear-gradient(180deg, #f8fafc 0%, #eef2f7 100%);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ------------------------------------------------------------- layout */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  gap: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 20;
}

.topbar-section {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.topbar-center {
  flex: 1 1 auto;
  justify-content: center;
}

.topbar-right {
  justify-content: flex-end;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 11px;
  color: #fff;
  background: linear-gradient(135deg, #6366f1, #8b5cf6 55%, #14b8a6);
  box-shadow: var(--shadow-sm);
  flex: none;
}

.brand-mark svg {
  width: 21px;
  height: 21px;
}

.brand-copy {
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 1.35;
  min-width: 0;
}

.brand-title {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 11.5px;
  padding-bottom: 1px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.badge-view {
  color: #7c2d12;
  background: #fff7ed;
  border: 1px solid #fed7aa;
}

/* ------------------------------------------------------------ toolbar */

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  flex-wrap: wrap;
  justify-content: center;
}

.toolbar[hidden] {
  display: none;
}

.tool-group {
  display: flex;
  gap: 4px;
}

.toolbar-divider {
  width: 1px;
  height: 26px;
  background: var(--border);
}

.tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 13px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  font-family: inherit;
}

.tool-btn svg {
  width: 17px;
  height: 17px;
}

.tool-btn:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

.tool-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.tool-btn[aria-pressed="true"] {
  background: var(--primary-soft);
  color: var(--primary-dark);
  border-color: #c7d2fe;
}

.tool-btn.danger:hover:not(:disabled) {
  background: var(--danger-soft);
  color: var(--danger);
}

.viewer-note {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
}

.viewer-note svg {
  width: 17px;
  height: 17px;
  color: var(--warning);
}

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

.swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid rgba(15, 23, 42, 0.12);
  cursor: pointer;
  padding: 0;
  position: relative;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.swatch:hover:not(:disabled) {
  transform: scale(1.12);
}

.swatch[aria-checked="true"] {
  box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--primary);
  transform: scale(1.06);
}

.swatch[aria-checked="true"]::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.85);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 600;
  font-family: inherit;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease, color 0.15s ease;
  text-decoration: none;
}

.btn svg {
  width: 16px;
  height: 16px;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn-primary {
  background: linear-gradient(180deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #4f46e5 0%, #4338ca 100%);
}

.btn-soft {
  background: var(--primary-soft);
  color: var(--primary-dark);
  border-color: #c7d2fe;
}

.btn-soft:hover:not(:disabled) {
  background: #e0e7ff;
}

.btn-ghost {
  background: var(--surface);
  color: var(--text-muted);
  border-color: var(--border);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

.btn-danger {
  background: linear-gradient(180deg, #f43f5e 0%, #e11d48 100%);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: linear-gradient(180deg, #e11d48 0%, #be123c 100%);
}

.btn-lg {
  padding: 12px 22px;
  font-size: 15px;
}

button:disabled,
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
}

.icon-btn svg {
  width: 16px;
  height: 16px;
}

.icon-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

/* --------------------------------------------------------- status pill */

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 13px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  box-shadow: 0 0 0 3px rgba(100, 116, 139, 0.15);
}

.status-pill[data-status="connected"] {
  color: #047857;
  background: #ecfdf5;
  border-color: #a7f3d0;
}

.status-pill[data-status="connected"] .status-dot {
  background: var(--success);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.18);
}

.status-pill[data-status="reconnecting"] {
  color: #b45309;
  background: #fffbeb;
  border-color: #fde68a;
}

.status-pill[data-status="reconnecting"] .status-dot {
  background: var(--warning);
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.2);
  animation: pulse 1.1s ease-in-out infinite;
}

.status-pill[data-status="error"] {
  color: #9f1239;
  background: var(--danger-soft);
  border-color: #fecdd3;
}

.status-pill[data-status="error"] .status-dot {
  background: var(--danger);
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.35;
  }
}

/* --------------------------------------------------------------- stage */

.stage {
  flex: 1 1 auto;
  display: grid;
  place-items: center;
  padding: 18px;
  min-height: 0;
}

.board-frame {
  position: relative;
  width: min(100%, calc((100vh - 190px) * 16 / 9));
  aspect-ratio: 16 / 9;
  max-height: 100%;
  background: #fff;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  background-image: radial-gradient(rgba(15, 23, 42, 0.055) 1px, transparent 1px);
  background-size: 26px 26px;
  background-position: -1px -1px;
}

body[data-tool="eraser"] #board {
  cursor: cell;
}

body[data-role="viewer"] #board,
body[data-connected="false"] #board {
  cursor: default;
}

.board-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(248, 250, 252, 0.72);
  backdrop-filter: blur(2px);
  z-index: 5;
}

.board-overlay[hidden] {
  display: none;
}

.overlay-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  max-width: 420px;
}

.overlay-card strong {
  font-size: 14.5px;
}

.overlay-card p {
  margin: 3px 0 0;
  font-size: 12.5px;
  color: var(--text-muted);
}

.spinner {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2.5px solid #e2e8f0;
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
  flex: none;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ----------------------------------------------------------- statusbar */

.statusbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 18px 14px;
  flex-wrap: wrap;
  justify-content: center;
}

.chip {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  white-space: nowrap;
}

.chip-muted {
  font-weight: 500;
  background: transparent;
  border-color: transparent;
  color: #94a3b8;
}

/* -------------------------------------------------------------- modals */

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(3px);
  z-index: 60;
  animation: fade 0.14s ease-out;
}

.modal-backdrop[hidden] {
  display: none;
}

@keyframes fade {
  from {
    opacity: 0;
  }
}

.modal {
  width: 100%;
  max-width: 520px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 24px;
  animation: rise 0.16s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes rise {
  from {
    transform: translateY(10px) scale(0.985);
    opacity: 0;
  }
}

.modal h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.01em;
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.modal-sub {
  margin: 5px 0 0;
  font-size: 13px;
  color: var(--text-muted);
}

.share-field + .share-field {
  margin-top: 20px;
}

.share-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 7px;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
}

.dot-edit {
  background: var(--primary);
}

.dot-view {
  background: var(--warning);
}

.share-row {
  display: flex;
  gap: 8px;
}

.share-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 10px 13px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.share-input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.share-note {
  margin: 18px 0 0;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  padding: 10px 12px;
}

.confirm-modal {
  max-width: 440px;
  text-align: center;
}

.confirm-icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  margin: 0 auto 14px;
  border-radius: 50%;
  background: var(--danger-soft);
  color: var(--danger);
}

.confirm-icon svg {
  width: 24px;
  height: 24px;
}

.confirm-modal p {
  margin: 8px 0 0;
  font-size: 13.5px;
  color: var(--text-muted);
  line-height: 1.55;
}

.confirm-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 22px;
}

/* --------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  padding: 11px 18px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.94);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  z-index: 90;
  animation: toastIn 0.18s ease-out;
}

.toast[hidden] {
  display: none;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
}

/* ---------------------------------------------------------- error page */

.error-page {
  overflow: auto;
}

.error-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.error-card {
  max-width: 520px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 40px 34px;
  text-align: center;
}

.error-icon {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  margin: 0 auto 18px;
  border-radius: 50%;
  background: var(--danger-soft);
  color: var(--danger);
}

.error-icon svg {
  width: 30px;
  height: 30px;
}

.error-card h1 {
  margin: 0 0 10px;
  font-size: 24px;
  letter-spacing: -0.02em;
}

.error-card p {
  margin: 0 0 10px;
  font-size: 14.5px;
  color: var(--text-muted);
  line-height: 1.6;
}

.error-hint {
  font-size: 12.5px !important;
}

.error-hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 12px;
}

.error-card .btn {
  margin-top: 18px;
}

/* --------------------------------------------------------- responsive */

@media (max-width: 900px) {
  .topbar {
    justify-content: center;
  }
  .topbar-center {
    order: 3;
    flex-basis: 100%;
  }
  .board-frame {
    width: 100%;
  }
  .brand-sub {
    max-width: 110px;
  }
}
