:root {
  --bg: #07080f;
  --bg-card: #ffffff;
  --ink: #e8eaf0;
  --ink-soft: #8890a5;
  --ink-dark: #1a1a2e;
  --accent: #7b9fff;
  --accent-glow: rgba(123, 159, 255, 0.25);
  --frost: rgba(180, 195, 230, 0.14);
  --grid-line: rgba(255, 255, 255, 0.06);
  --sheet-shadow: 0 -8px 40px rgba(0, 0, 0, 0.5);
  --overlay-shadow: 0 16px 60px rgba(0, 0, 0, 0.4);
  --radius: 20px;
  --radius-sm: 12px;
}

* { box-sizing: border-box; margin: 0; }

html {
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  font-family: "Manrope", sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* --- Top bar --- */

.top-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.8rem 1.2rem;
  padding-top: calc(0.8rem + env(safe-area-inset-top, 0px));
  background: linear-gradient(to bottom, rgba(7, 8, 15, 0.85) 0%, transparent 100%);
  pointer-events: none;
}

.top-btn {
  pointer-events: auto;
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: var(--ink);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.top-btn:hover { background: rgba(255, 255, 255, 0.14); }
.top-btn:active { transform: scale(0.95); }

.progress {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.progress-ring {
  width: 32px;
  height: 32px;
}

.progress-fill {
  transition: stroke-dashoffset 0.8s ease;
}

.progress-text {
  font-size: 0.72rem;
  letter-spacing: 0.15em;
  font-weight: 500;
  color: var(--ink-soft);
}

/* --- Canvas area --- */

.canvas-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  min-height: 100vh;
  padding: 60px 16px 100px;
  gap: 1rem;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  max-width: 480px;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 8px 60px rgba(0, 0, 0, 0.6);
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}

.canvas-wrap.is-zoomed {
  touch-action: none;
  cursor: grab;
}

.canvas-wrap.is-zoomed:active {
  cursor: grabbing;
}

.painting {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
}

/* --- Tile grid --- */

.tile-grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 1px;
  background: rgba(255, 255, 255, 0.45);
}

.tile {
  position: relative;
  cursor: pointer;
  backdrop-filter: blur(28px) saturate(0.5);
  -webkit-backdrop-filter: blur(28px) saturate(0.5);
  background: rgba(210, 215, 235, 0.22);
  transition: opacity 0.8s ease, background 0.3s ease;
}

.tile:hover:not(.revealed):not(.reserved) {
  background: rgba(220, 225, 245, 0.3);
}

.tile.selected {
  z-index: 2;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.7);
  background: rgba(123, 159, 255, 0.15);
}

.tile.reserved {
  cursor: default;
  animation: tile-pulse 1.5s ease-in-out infinite;
  background: rgba(255, 210, 130, 0.12);
}

@keyframes tile-pulse {
  0%, 100% { opacity: 0.85; }
  50% { opacity: 1; }
}

.tile.revealed {
  opacity: 0;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  background: transparent;
  pointer-events: none;
  cursor: default;
}

.tile.revealing {
  animation: tile-reveal 1s ease-out forwards;
}

@keyframes tile-reveal {
  0% { opacity: 1; background: rgba(210, 215, 235, 0.22); }
  15% { background: rgba(255, 255, 255, 0.35); }
  100% { opacity: 0; background: transparent; }
}

/* tile contributor indicator */
.tile-marker {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
}

.tile.revealed .tile-marker {
  opacity: 0.3;
  transition: opacity 0.3s;
}

/* --- Hint + actions below canvas --- */

.canvas-hint {
  font-size: 0.82rem;
  color: var(--ink-soft);
  letter-spacing: 0.08em;
  text-align: center;
}

.canvas-actions {
  display: flex;
  gap: 0.8rem;
  align-items: center;
}

.btn-random-small,
.btn-witnesses {
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: var(--ink-soft);
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-family: inherit;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}

.btn-random-small:hover,
.btn-witnesses:hover {
  border-color: rgba(255, 255, 255, 0.25);
  color: var(--ink);
}

.witness-count:not(:empty)::before { content: "("; }
.witness-count:not(:empty)::after { content: ")"; }

/* --- Bottom sheet --- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.sheet-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

.sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--bg-card);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--sheet-shadow);
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
}

.sheet.is-open {
  transform: translateY(0);
}

.sheet-handle {
  width: 36px;
  height: 4px;
  background: #d0d0d0;
  border-radius: 2px;
  margin: 10px auto 0;
}

.sheet-body {
  padding: 1.2rem 1.6rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  color: var(--ink-dark);
}

.sheet-title {
  font-family: "Cormorant Garamond", serif;
  font-size: 1.4rem;
  text-align: center;
}

.sheet-price-line {
  text-align: center;
  font-family: "Cormorant Garamond", serif;
  font-size: 2.2rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.sheet-fields {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.sheet-label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.78rem;
  color: #666;
}

.sheet-label input {
  border: 1px solid #e0e0e0;
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.8rem;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--ink-dark);
  transition: border-color 0.2s;
}

.sheet-label input:focus {
  outline: none;
  border-color: var(--accent);
}

.field-hint { color: #999; }

.sheet-pay {
  border: none;
  background: var(--ink-dark);
  color: #fff;
  padding: 0.9rem 1.4rem;
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}

.sheet-pay:hover { background: #2a2a50; }

.sheet-pay:disabled {
  opacity: 0.5;
  cursor: default;
}

.sheet-pay.is-loading {
  pointer-events: none;
  opacity: 0.7;
}

.sheet-pay.is-loading::after {
  content: "";
  display: inline-block;
  width: 0.7em;
  height: 0.7em;
  margin-left: 0.5em;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  vertical-align: middle;
  animation: spin 0.6s linear infinite;
}

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

.sheet-alt {
  border: none;
  background: none;
  color: var(--accent);
  font-family: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  padding: 0.3rem;
  text-align: center;
}

.sheet-alt:hover { text-decoration: underline; }

.sheet-note {
  font-size: 0.68rem;
  color: #999;
  text-align: center;
  letter-spacing: 0.04em;
}

/* --- Overlays (info, witnesses, completion) --- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.overlay.is-open {
  opacity: 1;
  pointer-events: auto;
}

.overlay-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.overlay-card {
  position: relative;
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 2rem 1.6rem;
  max-width: 420px;
  width: 100%;
  box-shadow: var(--overlay-shadow);
  color: var(--ink-dark);
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  transform: translateY(16px);
  transition: transform 0.3s ease;
  max-height: 80vh;
  max-height: 80dvh;
  overflow-y: auto;
}

.overlay.is-open .overlay-card {
  transform: translateY(0);
}

.overlay-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  border: none;
  background: none;
  color: #999;
  cursor: pointer;
  padding: 0.2rem;
  transition: color 0.2s;
}

.overlay-close:hover { color: var(--ink-dark); }

.overlay-card h2 {
  font-family: "Cormorant Garamond", serif;
  font-size: 1.8rem;
  letter-spacing: 0.02em;
  padding-right: 2rem;
}

.overlay-card p {
  line-height: 1.7;
  font-size: 0.92rem;
  color: #555;
}

.overlay-card hr {
  border: none;
  border-top: 1px solid #eee;
  margin: 0.4rem 0;
}

.info-meaning {
  font-style: italic;
  color: #777 !important;
  font-size: 0.88rem !important;
}

.info-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 0.4rem;
}

.info-raised {
  font-size: 0.78rem !important;
  font-weight: 500;
  color: var(--ink-dark) !important;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.info-link {
  font-size: 0.78rem;
  color: var(--accent);
  text-decoration: none;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.info-link:hover { text-decoration: underline; }

/* --- Witnesses drawer --- */

.overlay-drawer {
  max-height: 70vh;
  max-height: 70dvh;
}

.drawer-raised {
  font-size: 0.75rem !important;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent) !important;
  font-weight: 500;
}

.witness-list {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  overflow-y: auto;
}

.witness-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0;
  border-bottom: 1px solid #f0f0f0;
  font-size: 0.85rem;
}

.witness-name {
  font-weight: 500;
  color: var(--ink-dark);
}

.witness-name a {
  color: var(--accent);
  text-decoration: none;
}

.witness-name a:hover { text-decoration: underline; }

.witness-meta {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.72rem;
  color: #999;
}

.witness-tiles {
  background: #f5f5f5;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-weight: 500;
  color: #666;
}

.witness-empty {
  text-align: center;
  padding: 2rem 0;
  color: #aaa;
  font-size: 0.85rem;
}

/* --- Completion --- */

.overlay-complete-card {
  text-align: center;
  padding: 3rem 2rem;
}

.overlay-complete-card h2 {
  font-size: 2.4rem;
  padding-right: 0;
}

.complete-thanks {
  font-weight: 500;
  color: var(--ink-dark) !important;
}

.overlay-close-btn {
  border: none;
  background: var(--ink-dark);
  color: #fff;
  padding: 0.9rem 2rem;
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
  margin-top: 0.5rem;
}

.overlay-close-btn:hover { background: #2a2a50; }

/* --- Toast --- */

.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  z-index: 70;
  background: var(--bg-card);
  color: var(--ink-dark);
  padding: 0.8rem 1.4rem;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 500;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
  white-space: nowrap;
  transition: transform 0.4s cubic-bezier(0.32, 0.72, 0, 1);
  pointer-events: none;
}

.toast.is-visible {
  transform: translateX(-50%) translateY(0);
}

/* --- Animations --- */

body.is-loaded .canvas-wrap {
  animation: fade-up 0.8s ease both;
}

body.is-loaded .canvas-hint {
  animation: fade-up 0.8s ease 0.1s both;
}

body.is-loaded .canvas-actions {
  animation: fade-up 0.8s ease 0.2s both;
}

@keyframes fade-up {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* grid complete state */
.tile-grid.is-complete {
  animation: grid-fade 1.5s ease forwards;
}

@keyframes grid-fade {
  to { opacity: 0; }
}

/* --- Responsive --- */

@media (min-width: 600px) {
  .canvas-wrap {
    max-width: 520px;
  }
}

@media (min-width: 900px) {
  .canvas-area {
    padding: 80px 24px 60px;
  }

  .canvas-wrap {
    max-width: 560px;
  }

  .top-bar {
    padding: 1rem 2rem;
  }
}

@media (max-width: 380px) {
  .canvas-wrap {
    border-radius: var(--radius-sm);
  }

  .sheet-body {
    padding: 1rem 1.2rem 1.5rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }

  .tile.revealed {
    opacity: 0;
  }
}

/* Modal open state */
body.sheet-open,
body.overlay-open {
  overflow: hidden;
}
