:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --border: #e3e6ea;
  --border-strong: #cfd4da;
  --text: #1b1f24;
  --text-2: #5b6470;
  --text-3: #8a929c;
  --accent: #3b5bdb;
  --accent-hover: #3049c0;
  --accent-soft: #edf2ff;
  --danger: #e03131;
  --success: #2f9e44;
  --warning: #f08c00;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 4px 12px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 12px 40px rgba(16, 24, 40, 0.18);
  --ui-font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --doc-font: "Source Serif 4", Georgia, "Times New Roman", serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--ui-font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 36px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
  font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { color: var(--danger); }
.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn-icon { width: 34px; padding: 0; }
.btn svg { width: 16px; height: 16px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.input, select.input {
  width: 100%; height: 40px; padding: 0 12px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
}
.input:focus { border-color: var(--accent); }
label.field { display: block; margin-bottom: 14px; }
label.field span { display: block; font-weight: 500; font-size: 13px; margin-bottom: 6px; color: var(--text-2); }

/* ---------- Brand & top bar ---------- */
.brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; color: var(--text); letter-spacing: -0.01em; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center;
  background: linear-gradient(135deg, #4c6ef5, #3b5bdb 55%, #364fc7); color: #fff;
  box-shadow: 0 2px 6px rgba(59, 91, 219, .35);
}
.brand-mark svg { width: 18px; height: 18px; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  height: 64px; padding: 0 24px;
  background: rgba(255, 255, 255, .92); backdrop-filter: saturate(180%) blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar .spacer { flex: 1; }

.avatar {
  --avatar-color: #868e96;
  display: inline-grid; place-items: center; flex: none;
  border-radius: 50%; background: var(--avatar-color); color: #fff;
  font-weight: 600; letter-spacing: .02em; user-select: none;
  box-shadow: 0 0 0 2px var(--surface);
}
.user-chip { display: inline-flex; align-items: center; gap: 10px; padding: 4px 4px 4px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); }
.user-chip .who { line-height: 1.2; }
.user-chip .who b { display: block; font-size: 13px; }
.user-chip .who small { color: var(--text-3); font-size: 12px; }

/* ---------- Sign-in ---------- */
.auth-wrap { min-height: 100vh; display: grid; grid-template-columns: 1.1fr 1fr; }
.auth-hero {
  position: relative; overflow: hidden;
  padding: 56px; color: #fff; display: flex; flex-direction: column; justify-content: space-between;
  background: radial-gradient(1200px 600px at 10% 10%, #5c7cfa 0%, transparent 60%),
              radial-gradient(900px 500px at 90% 90%, #7048e8 0%, transparent 55%),
              #2b3a99;
}
.auth-hero .brand { color: #fff; }
.auth-hero .brand-mark { background: rgba(255,255,255,.16); box-shadow: none; }
.auth-hero h1 { font-size: 44px; line-height: 1.1; letter-spacing: -0.02em; margin: 0 0 16px; max-width: 520px; }
.auth-hero p { font-size: 17px; opacity: .85; max-width: 460px; margin: 0; }
.hero-card {
  margin-top: 40px; max-width: 460px; background: #fff; color: var(--text); border-radius: 14px;
  box-shadow: var(--shadow-lg); padding: 22px 24px; font-family: var(--doc-font); font-size: 15px; line-height: 1.7;
}
.hero-card .hc-bar { display: flex; gap: 6px; margin: -6px 0 12px; font-family: var(--ui-font); }
.hero-card .hc-bar .avatar { width: 24px; height: 24px; font-size: 10px; }
.hc-caret { display: inline-block; width: 2px; height: 1.15em; vertical-align: text-bottom; position: relative; background: var(--c); animation: blink 1.1s steps(1) infinite; }
.hc-caret::after { content: attr(data-name); position: absolute; bottom: 100%; left: -1px; background: var(--c); color: #fff; font: 600 10px/1 var(--ui-font); padding: 3px 5px; border-radius: 3px 3px 3px 0; white-space: nowrap; }
.hc-sel { background: color-mix(in srgb, var(--c) 22%, transparent); }
@keyframes blink { 50% { opacity: .35; } }
.auth-hero footer { font-size: 13px; opacity: .7; }

.auth-panel { display: grid; place-items: center; padding: 40px 24px; background: var(--surface); }
.auth-card { width: 100%; max-width: 380px; }
.auth-card h2 { font-size: 24px; margin: 0 0 6px; letter-spacing: -0.01em; }
.auth-card .sub { color: var(--text-2); margin: 0 0 28px; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--text-3); font-size: 12px; margin: 24px 0 16px; text-transform: uppercase; letter-spacing: .06em; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.user-list { display: grid; gap: 6px; max-height: 220px; overflow: auto; }
.user-row { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border-radius: 8px; border: 1px solid transparent; background: none; text-align: left; cursor: pointer; width: 100%; }
.user-row:hover { background: var(--surface-2); border-color: var(--border); }
.user-row b { display: block; font-weight: 500; }
.user-row small { color: var(--text-3); }
.demo-callout { margin-top: 24px; padding: 16px; border-radius: 12px; background: var(--accent-soft); border: 1px solid #dbe4ff; }
.demo-callout b { display: block; margin-bottom: 4px; }
.demo-callout p { margin: 0 0 12px; color: var(--text-2); font-size: 13px; }

/* ---------- Dashboard ---------- */
.page { max-width: 1120px; margin: 0 auto; padding: 32px 24px 64px; }
.section-title { display: flex; align-items: center; justify-content: space-between; margin: 0 0 16px; }
.section-title h2 { font-size: 16px; margin: 0; font-weight: 600; }
.templates { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; margin-bottom: 40px; }
.template { background: none; border: none; padding: 0; cursor: pointer; text-align: left; }
.template .thumb {
  aspect-ratio: 4 / 3.2; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border);
  box-shadow: var(--shadow-sm); display: grid; place-items: center; transition: border-color .12s, box-shadow .12s, transform .12s;
  padding: 16px; overflow: hidden;
}
.template:hover .thumb { border-color: var(--accent); box-shadow: var(--shadow); transform: translateY(-1px); }
.template .thumb .plus { width: 44px; height: 44px; color: var(--accent); }
.template .lines { width: 100%; align-self: start; display: grid; gap: 7px; }
.template .lines i { display: block; height: 6px; border-radius: 3px; background: var(--surface-2); }
.template .lines i.h { height: 9px; width: 60%; background: #dee2e6; margin-bottom: 4px; }
.template span { display: block; margin-top: 10px; font-weight: 500; }
.template small { color: var(--text-3); }

.doc-table { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); overflow: hidden; }
.doc-row { display: grid; grid-template-columns: minmax(0, 2.4fr) 1.2fr 1fr 110px 96px; align-items: center; gap: 16px; padding: 12px 18px; border-top: 1px solid var(--border); }
.doc-row:first-child { border-top: none; }
.doc-row.head { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); background: #fafbfc; padding-top: 10px; padding-bottom: 10px; }
.doc-row:not(.head) { cursor: pointer; transition: background .1s; }
.doc-row:not(.head):hover { background: #fafbff; }
.doc-title { display: flex; align-items: center; gap: 12px; min-width: 0; }
.doc-title .icon { width: 34px; height: 34px; flex: none; border-radius: 8px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.doc-title .icon svg { width: 18px; height: 18px; }
.doc-title b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--text-2); }
.owner { display: flex; align-items: center; gap: 8px; min-width: 0; }
.owner span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--text-2); }
.badge.edit { background: #ebfbee; color: #2b8a3e; }
.badge.view { background: #fff4e6; color: #d9480f; }
.badge.owner-b { background: var(--accent-soft); color: var(--accent); }
.row-actions { display: flex; justify-content: flex-end; gap: 2px; }
.empty { padding: 56px 24px; text-align: center; color: var(--text-2); }
.empty svg { width: 44px; height: 44px; color: var(--text-3); margin-bottom: 8px; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(16, 24, 40, .45); display: grid; place-items: center; z-index: 100; padding: 16px; animation: fade .15s ease-out; }
.modal { width: 100%; max-width: 480px; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); padding: 24px; animation: pop .18s ease-out; }
.modal h3 { margin: 0 0 4px; font-size: 18px; }
.modal .sub { margin: 0 0 18px; color: var(--text-2); }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.share-list { display: grid; gap: 4px; max-height: 320px; overflow: auto; margin: 0 -8px; }
.share-row { display: flex; align-items: center; gap: 12px; padding: 8px; border-radius: 8px; }
.share-row:hover { background: var(--surface-2); }
.share-row .who { flex: 1; min-width: 0; line-height: 1.25; }
.share-row .who b { display: block; font-weight: 500; }
.share-row .who small { color: var(--text-3); }
.share-row select { width: auto; height: 32px; font-size: 13px; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #212529; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow-lg); z-index: 200;
  transition: opacity .2s, transform .2s; font-weight: 500;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast[data-kind="error"] { background: var(--danger); }

/* ---------- Editor ---------- */
.editor-body { background: #eef0f3; min-height: 100vh; }
.editor-bar { height: 60px; gap: 12px; padding: 0 16px; }
.editor-bar .back { width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center; color: var(--text-2); }
.editor-bar .back:hover { background: var(--surface-2); }
.title-group { display: flex; flex-direction: column; min-width: 0; }
.title-input {
  border: 1px solid transparent; border-radius: 6px; padding: 2px 6px; margin-left: -6px;
  font-size: 17px; font-weight: 600; background: transparent; min-width: 120px; max-width: 420px; text-overflow: ellipsis;
}
.title-input:hover:not(:read-only) { border-color: var(--border); }
.title-input:focus { outline: none; border-color: var(--accent); background: #fff; }
.save-state { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-3); padding-left: 1px; }
.save-state .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); }
.save-state[data-state="saved"] .dot { background: var(--success); }
.save-state[data-state="saving"] .dot { background: var(--warning); animation: blink 1s infinite; }
.save-state[data-state="offline"] { color: var(--danger); }
.save-state[data-state="offline"] .dot { background: var(--danger); }

.presence { display: flex; align-items: center; }
.presence .avatar { margin-left: -6px; transition: transform .15s; cursor: default; position: relative; }
.presence .avatar:first-child { margin-left: 0; }
.presence .avatar:hover { transform: translateY(-2px); z-index: 2; }
.presence .avatar.you { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--avatar-color); }
.presence-label { font-size: 12px; color: var(--text-2); margin-left: 10px; white-space: nowrap; }
.live-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--success); margin-right: 5px; box-shadow: 0 0 0 3px rgba(47, 158, 68, .18); }

.banner { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 8px 16px; font-size: 13px; background: #fff4e6; color: #d9480f; border-bottom: 1px solid #ffe8cc; }

.canvas { padding: 32px 16px 96px; }
.paper {
  position: relative; max-width: 816px; margin: 0 auto; background: #fff;
  border-radius: 4px; box-shadow: 0 1px 3px rgba(16,24,40,.08), 0 8px 24px rgba(16,24,40,.06);
  min-height: 1056px; padding: 72px 88px;
}
.doc-text, .doc-mirror {
  font-family: var(--doc-font); font-size: 16.5px; line-height: 1.75; letter-spacing: 0;
  white-space: pre-wrap; overflow-wrap: break-word; word-break: normal; tab-size: 4;
  margin: 0; padding: 0; border: 0; width: 100%;
}
.doc-text {
  display: block; resize: none; outline: none; background: transparent; color: var(--text);
  overflow: hidden; min-height: 900px; caret-color: var(--accent);
}
.doc-text::placeholder { color: var(--text-3); }
.doc-mirror {
  position: absolute; top: 72px; left: 88px; right: 88px; width: auto;
  color: transparent; pointer-events: none; user-select: none;
}
.r-sel { background: color-mix(in srgb, var(--c) 24%, transparent); border-radius: 2px; }
.r-caret { position: relative; display: inline-block; width: 0; height: 1em; }
.r-caret::before {
  content: ""; position: absolute; left: -1px; top: -0.2em; width: 2px; height: 1.45em; background: var(--c); border-radius: 1px;
}
.r-caret .r-flag {
  position: absolute; left: -1px; bottom: calc(1.25em); transform-origin: bottom left;
  background: var(--c); color: #fff; font: 600 11px/1 var(--ui-font); padding: 4px 6px; border-radius: 4px 4px 4px 0;
  white-space: nowrap; transition: opacity .3s, transform .3s; box-shadow: 0 1px 2px rgba(0,0,0,.15);
}
.r-caret.idle .r-flag { opacity: 0; transform: scale(.6); }
.r-caret.idle::after { content: ""; position: absolute; left: -3px; top: -0.35em; width: 6px; height: 6px; background: var(--c); border-radius: 1px; }

.statusbar {
  position: fixed; bottom: 16px; right: 16px; display: flex; gap: 12px; align-items: center;
  background: rgba(255,255,255,.95); border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px;
  font-size: 12px; color: var(--text-2); box-shadow: var(--shadow);
}

/* Embedded editor (used inside the multi-user demo) */
.embed .canvas { padding: 16px 12px 40px; }
.embed .paper { padding: 36px 32px; min-height: 600px; box-shadow: 0 1px 3px rgba(16,24,40,.08); }
.embed .doc-mirror { top: 36px; left: 32px; right: 32px; }
.embed .doc-text { min-height: 520px; }
.embed .doc-text, .embed .doc-mirror { font-size: 14.5px; }
.embed .statusbar { display: none; }
.embed .editor-bar { height: 52px; }
.embed .title-input { font-size: 15px; max-width: 220px; }
.embed .hide-embed { display: none !important; }

/* ---------- Demo split view ---------- */
.demo-body { height: 100vh; display: flex; flex-direction: column; background: #e9ecef; }
.demo-head { display: flex; align-items: center; gap: 16px; padding: 12px 20px; background: #fff; border-bottom: 1px solid var(--border); }
.demo-head h1 { font-size: 15px; margin: 0; font-weight: 600; }
.demo-head p { margin: 0; color: var(--text-2); font-size: 13px; }
.demo-grid { flex: 1; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; padding: 14px; min-height: 0; }
.pane { display: flex; flex-direction: column; min-height: 0; background: #fff; border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); border: 1px solid var(--border); }
.pane-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--border); background: #fbfbfc; }
.pane-head .avatar { width: 28px; height: 28px; font-size: 11px; }
.pane-head b { font-size: 13px; display: block; line-height: 1.2; }
.pane-head small { font-size: 11px; color: var(--text-3); }
.pane-head .spacer { flex: 1; }
.pane iframe { flex: 1; width: 100%; border: 0; background: #eef0f3; }
.window-dots { display: flex; gap: 5px; margin-right: 4px; }
.window-dots i { width: 10px; height: 10px; border-radius: 50%; background: #dee2e6; }
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; user-select: none; }
.switch input { appearance: none; width: 34px; height: 20px; border-radius: 999px; background: var(--border-strong); position: relative; cursor: pointer; transition: background .15s; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(14px); }

@media (max-width: 960px) {
  .auth-wrap { grid-template-columns: 1fr; }
  .auth-hero { padding: 32px 24px; }
  .auth-hero h1 { font-size: 32px; }
  .hero-card { display: none; }
  .doc-row { grid-template-columns: minmax(0, 1fr) 96px; }
  .doc-row > :nth-child(2), .doc-row > :nth-child(3), .doc-row > :nth-child(4) { display: none; }
  .demo-grid { grid-template-columns: 1fr; overflow: auto; }
  .pane { min-height: 520px; }
  .paper { padding: 40px 24px; }
  .doc-mirror { top: 40px; left: 24px; right: 24px; }
  .presence-label { display: none; }
}

/* ---------- Guest mode ---------- */
.guest-btn { width: 100%; height: 42px; margin-top: 10px; color: var(--text-2); }
.guest-btn:hover { color: var(--text); }
.guest-banner {
  display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--accent-soft); border-bottom: 1px solid #dbe4ff; color: #364fc7; font-size: 13px;
}

/* ---------- Phones ---------- */
@media (max-width: 640px) {
  .topbar { gap: 8px; padding: 0 12px; }
  .editor-bar { gap: 8px; padding: 0 10px; }
  .btn-label, .long-label, .user-chip .who { display: none; }
  #dash-demo { width: 40px; padding: 0; justify-content: center; }
  .user-chip { padding: 2px; gap: 0; }
  .title-input { min-width: 0; width: 100%; }
  .guest-banner { padding: 10px 16px; }
  /* demo header: logo + back on one line, then the title, then the switch */
  .demo-head { flex-wrap: wrap; gap: 10px 12px; padding: 12px; }
  .demo-head > div:not(.spacer) { order: 1; flex-basis: 100%; }
  .demo-head .switch { order: 2; }
}
/* Touch screens: bigger tap targets, and 16px+ text in fields so iOS Safari doesn't zoom on focus */
@media (pointer: coarse) {
  .btn-sm { height: 40px; padding: 0 12px; }
  .btn-icon { width: 40px; }
  .editor-bar .back { width: 40px; height: 40px; flex: none; }
  .input, select.input { font-size: 16px; }  /* the editor keeps its size: .doc-mirror must match it */
}
