*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--color-text-primary);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  overflow-x: hidden;
  overflow-y: auto;
}

a { color: inherit; text-decoration: none; }
button { border: none; background: none; cursor: pointer; font: inherit; color: inherit; }
input, textarea { font: inherit; color: inherit; }

/* Top Bar */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-lg);
  z-index: 100;
}

.topbar-brand {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.topbar-logo {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  flex-shrink: 0;
}

.topbar-search-btn {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-secondary);
}
.topbar-search-btn svg {
  width: 20px;
  height: 20px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

.topbar-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  font-size: 17px;
  color: var(--color-text-secondary);
  transition: background var(--duration-fast) ease, color var(--duration-fast) ease;
}

.topbar-btn:active {
  background: var(--color-surface-raised);
  color: var(--color-text-primary);
}

/* Main Scroll Area */
.main-scroll {
  padding-top: var(--topbar-height);
  padding-bottom: calc(var(--bottom-safe-area) + 80px);
  min-height: 100dvh;
}

/* Bottom Safe Area */
.safe-area-bottom {
  height: calc(var(--bottom-safe-area) + 60px);
}

/* Overlay */
.overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--color-overlay);
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-normal) var(--ease-out);
}

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

/* Loader */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-4xl);
  gap: var(--space-lg);
  color: var(--color-text-muted);
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.error-state {
  text-align: center;
  padding: var(--space-3xl);
  color: var(--color-text-muted);
}

.error-state .retry-btn {
  margin-top: var(--space-lg);
  padding: var(--space-sm) var(--space-xl);
  background: var(--color-surface-raised);
  border-radius: var(--radius-md);
  color: var(--color-text-primary);
  border: 1px solid var(--color-border);
}

/* Auth Modal */
.auth-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-overlay);
  opacity: 0;
  pointer-events: none;
  touch-action: manipulation;
  transition: opacity var(--duration-normal) var(--ease-out);
}
.auth-modal.active {
  opacity: 1;
  pointer-events: auto;
}
.auth-box {
  max-height: 80vh;
  overflow-y: auto;
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: 28px 24px 24px;
  width: 320px;
  max-width: 90vw;
  position: relative;
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  transition: transform 0.2s ease;
}
.auth-box.shake {
  animation: shake 0.3s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}
.auth-modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 28px; height: 28px;
  border-radius: 50%;
  background: rgba(0,0,0,0.06);
  border: none;
  font-size: 18px;
  color: var(--color-text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.auth-modal-close:active {
  background: rgba(0,0,0,0.15);
}
.auth-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--color-border);
}
.auth-tab {
  flex: 1;
  padding: 10px 0 8px;
  text-align: center;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.auth-tab.active {
  color: var(--color-accent);
  border-bottom-color: var(--color-accent);
}
.auth-view { display: none; }
.auth-view.active { display: flex; flex-direction: column; gap: 12px; }
.auth-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  font-size: var(--text-sm);
  color: var(--color-text-primary);
  outline: none;
  transition: border-color 0.15s;
}
.auth-input:focus {
  border-color: var(--color-accent);
}
.auth-btn { touch-action: manipulation;
  width: 100%;
  padding: 12px;
  background: var(--color-accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
  text-align: center;
}
.auth-btn:active { opacity: 0.8; }
.auth-btn.loading { opacity: 0.6; pointer-events: none; }
.auth-error {
  font-size: var(--text-xs);
  color: #d43c33;
  display: none;
}
.auth-logout-btn {
  background: #d43c33;
  margin-top: 8px;
}
.auth-avatar {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin: 0 auto;
}
.auth-nickname {
  font-size: var(--text-lg);
  font-weight: 600;
  text-align: center;
}
.auth-email {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  text-align: center;
}

/* Account button in topbar when logged in */
.topbar-btn.logged-in {
  color: var(--color-accent);
}

/* Protected workspaces stay out of the guest document flow. */
.dashboard {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  flex-direction: column;
  background: var(--color-bg);
}

.quiz-overlay { display: none; }
.quiz-overlay.active { display: flex; }
