:root {
  --bg: #0b0e13;
  --panel: #11161e;
  --muted: #93a0b4;
  --text: #e6edf7;
  --accent: #7aa2f7;
  --accent-2: #34d399;
  --ring: #3b82f6;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #10b981;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --radius: 14px;
  --gap: 12px;
  --font: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu,
    Cantarell, "Noto Sans", "Helvetica Neue", Arial, "Apple Color Emoji",
    "Segoe UI Emoji";
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
  --toolbar-h: 56px;
  --status-h: 38px;
  --editor-max: 1000px;
  --editor-font-size: 18px;   /* try 17px / 18px */
  --editor-line-height: 1.6; /* comfy body copy */
  /* paragraph gap scales with line-height and font size (em) */
  --editor-para-gap: calc(0.6em * var(--editor-line-height));
}
html.light {
  --bg: #f6f7fb;
  --panel: #fff;
  --muted: #5b6678;
  --text: #0b1220;
  --accent: #1f6fff;
  --accent-2: #0ea5e9;
  --ring: #1f6fff;
  --shadow: 0 10px 30px rgba(2, 6, 23, 0.12);
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7fb;
    --panel: #fff;
    --muted: #5b6678;
    --text: #0b1220;
    --accent: #1f6fff;
    --accent-2: #0ea5e9;
    --ring: #1f6fff;
    --shadow: 0 10px 30px rgba(2, 6, 23, 0.12);
  }
}

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--font);
  overflow: hidden;
}

.wrap {
  min-height: 100%;
  display: grid;
  grid-template-rows: var(--toolbar-h) 1fr var(--status-h);
}
.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  height: var(--toolbar-h);
  background: linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.02),
      rgba(255, 255, 255, 0)
    ),
    var(--panel);
  box-shadow: var(--shadow);
  border-bottom: 1px solid rgba(148, 163, 184, 0.18);
  /* reserve some room on the right for the title pill so centered tools don't overlap */
  padding-right: clamp(180px, 28ch, 32vw);
}
/* wrapper that holds all buttons/groups in the center */
.tools {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  width: max-content; /* shrink-wrap around buttons */
}

/* when the title input is visible (editing), let the tools go back into normal flow and take available space */
.toolbar:has(.title:not(.hidden)) .tools,
.toolbar:has(#docTitle:focus) .tools {
  position: static;
  transform: none;
  flex: 1 1 auto;
  justify-content: flex-start;
}
/* turn off the right-side padding while the title is visible/focused */
.toolbar:has(.title:not(.hidden)),
.toolbar:has(#docTitle:focus) {
  padding-right: 8px; /* matches your left padding */
}
.title {
  width: 26ch;
  max-width: 32vw;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: var(--text);
  outline: none;
}
.title.hidden {
  display: none;
}
.title::placeholder {
  color: color-mix(in oklch, var(--muted) 70%, transparent);
}
/* Non-editable title pill */
.title-view {
  display: inline-flex;
  align-items: center;
  max-width: 32vw;
  height: 36px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: rgba(148, 163, 184, 0.08);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title-view.hidden {
  display: none;
}
/* keep the title controls pinned to the right */
.title-view,
.title {
  margin-left: auto;
}
#titleText {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* Mobile toolbar: scrollable, not clipped */
@media (max-width: 640px) {
  .toolbar {
    overflow-x: auto; /* allow horizontal scroll */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-right: 8px; /* remove the big reserved space on small screens */
  }
  .toolbar::-webkit-scrollbar {
    display: none;
  }

  .tools {
    position: static; /* no absolute centering on mobile */
    transform: none;
    min-width: max-content; /* ensures scroll width matches content */
    flex-wrap: nowrap;
  }

  /* optional: tighten spacing so more buttons fit */
  .toolbar .group {
    gap: 6px;
  }
  .btn,
  .seg button {
    width: 32px;
    height: 32px;
  }
}

.spacer {
  flex: 1;
}
.btn,
.seg button {
  display: inline-grid;
  place-items: center;
  height: 36px;
  width: 36px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s;
}
.btn:hover,
.seg button:hover {
  background: rgba(148, 163, 184, 0.12);
}
.btn:active,
.seg button:active {
  transform: translateY(1px);
}

.seg {
  display: flex;
  gap: 6px;
  padding: 0;
  background: transparent;
  border: 0;
}

/* Color input should fill its whole container */
.color {
  appearance: none;
  -webkit-appearance: none;
  padding: 0;
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 10px;
}
.color::-webkit-color-swatch-wrapper {
  padding: 0;
}
.color::-webkit-color-swatch {
  border: none;
  border-radius: 10px;
}
.color::-moz-focus-inner {
  border: 0;
  padding: 0;
}
.color::-moz-color-swatch {
  border: none;
  border-radius: 10px;
}
.seg[aria-label="Headings"] button {
  width: auto;
  padding: 0 10px;
  font-weight: 600;
}
.btn[data-active="true"],
.seg button[data-active="true"] {
  outline: 2px solid color-mix(in oklch, var(--ring) 60%, transparent);
}
.btn svg {
  width: 18px;
  height: 18px;
}
.color {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  padding: 0;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  cursor: pointer;
}
.toolbar .group {
  display: flex;
  gap: 8px;
  align-items: center;
}
.divider {
  width: 1px;
  height: 28px;
  background: rgba(148, 163, 184, 0.25);
}

.surface {
  display: flex;
  justify-content: center;
  padding: 24px;
}
.editor {
  font-size: var(--editor-font-size);
  line-height: var(--editor-line-height);
  width: min(100%, var(--editor-max));
  min-height: calc(100dvh - var(--toolbar-h) - var(--status-h) - 48px);
  background: var(--panel);
  border-radius: var(--radius);
  padding: clamp(18px, 2vw, 28px);
  padding-inline: clamp(28px, 6vw, 56px);
  box-shadow: var(--shadow);
  outline: none;
  border: 1px solid rgba(148, 163, 184, 0.18);
}
.editor:focus {
  box-shadow: 0 0 0 2px color-mix(in oklch, var(--ring) 50%, transparent),
    var(--shadow);
}
.editor:empty:before {
  content: "Start writing…";
  color: var(--muted);
}

/* Typography */
.editor h1 {
  font-size: clamp(28px, 3.2vw, 36px);
  line-height: 1.2;
  margin: 0.6em 0 0.4em;
}
.editor h2 {
  font-size: clamp(22px, 2.6vw, 28px);
  line-height: 1.25;
  margin: 0.8em 0 0.4em;
}
.editor h3 {
  font-size: clamp(18px, 2.2vw, 22px);
  line-height: 1.3;
  margin: 0.9em 0 0.5em;
}
.editor p {
  margin: 0;
}
.editor > p + p {
  margin-top: var(--editor-para-gap);
}
.editor blockquote {
  border-left: 4px solid color-mix(in oklch, var(--accent) 70%, transparent);
  padding: 0.4em 0.8em;
  margin: 0.6em 0;
  color: color-mix(in oklch, var(--text) 85%, #8aa2bd);
  background: color-mix(in oklch, var(--accent) 8%, transparent);
  border-radius: 8px;
}
.editor pre {
  background: #0e1726;
  color: #d1e7ff;
  padding: 12px 14px;
  border-radius: 10px;
  font-family: var(--mono);
  overflow: auto;
}
.editor code {
  font-family: var(--mono);
  background: color-mix(in oklch, var(--accent) 15%, transparent);
  padding: 0.1em 0.35em;
  border-radius: 6px;
}
.editor a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.editor img {
  max-width: 100%;
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.editor mark {
  background: color-mix(in oklch, var(--warn) 60%, transparent);
  color: inherit;
  padding: 0.06em 0.25em;
  border-radius: 0.25em;
}
.editor ul,
.editor ol {
  padding-left: 1.1em;
}
.editor hr {
  border: none;
  border-top: 1px dashed rgba(148, 163, 184, 0.3);
  margin: 1em 0;
}

/* Status */
.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: var(--status-h);
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid rgba(148, 163, 184, 0.18);
  background: var(--panel);
  position: sticky;
  bottom: 0;
  z-index: 5;
}
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.12);
  border: 1px solid rgba(148, 163, 184, 0.24);
  color: var(--muted);
}
/* Status: sound button */
.badge.icon {
  padding: 4px; /* smaller padding for a pure icon */
  line-height: 0;
  cursor: pointer;
  user-select: none;
}
.badge.icon svg {
  width: 18px;
  height: 18px;
  display: inline-block;
}

/* show only one icon based on muted state */
#soundToggle [data-ico="off"] {
  display: none;
}
#soundToggle[data-muted="true"] [data-ico="on"] {
  display: none;
}
#soundToggle[data-muted="true"] [data-ico="off"] {
  display: inline-block;
}
/* Floater */
.floater-link {
  position: fixed;
  right: 10px;
  bottom: 56px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 999px;
  background: linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.06),
      rgba(255, 255, 255, 0)
    ),
    var(--panel);
  border: 1px solid rgba(148, 163, 184, 0.25);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0.9;
  transition: transform 0.08s ease, opacity 0.15s, background 0.2s;
  user-select: none;
}
.floater-link:hover {
  transform: translateY(-1px);
  opacity: 1;
}
.floater-link svg {
  width: 16px;
  height: 16px;
}

.btn:focus {
  outline: none;
}
.btn:focus-visible {
  outline: 2px solid color-mix(in oklch, var(--ring) 60%, transparent);
}

@media print {
  .toolbar,
  .status,
  .floater-link {
    display: none !important;
  }
  body {
    background: #fff;
  }
  .editor {
    box-shadow: none;
    border: none;
  }
}
/* forced themes always win */
html[data-theme="dark"] {
  --bg: #0b0e13;
  --panel: #11161e;
  --muted: #93a0b4;
  --text: #e6edf7;
  --accent: #7aa2f7;
  --accent-2: #34d399;
  --ring: #3b82f6;
  --shadow: 0 10px 30px rgba(0, 0, 0, 35);
}
html[data-theme="light"] {
  --bg: #f6f7fb;
  --panel: #fff;
  --muted: #5b6678;
  --text: #0b1220;
  --accent: #1f6fff;
  --accent-2: #0ea5e9;
  --ring: #1f6fff;
  --shadow: 0 10px 30px rgba(2, 6, 23, 12);
}
/* --- Clippings launcher button --- */
.clips-btn {
  position: fixed;
  left: 12px;
  /* Sit above status bar + the "Canvas" floater link */
  bottom: calc(env(safe-area-inset-bottom) + 16px);

  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 999px;

  background: var(--panel, #ffffff);
  color: var(--text, #0b1220);
  border: 1px solid rgba(148, 163, 184, 0.25);
  box-shadow: 0 6px 18px rgba(2, 6, 23, 0.14);

  font-weight: 600;
  text-decoration: none;
  user-select: none;
  cursor: pointer;

  /* Be on top of sticky bars / transforms */
  z-index: 10000;

  /* nice micro-interaction */
  transition: transform 0.08s ease, opacity 0.15s, background 0.2s;
  opacity: 0.96;
}
.clips-btn:hover {
  transform: translateY(-1px);
  opacity: 1;
}
.clips-btn svg {
  width: 16px;
  height: 16px;
  display: block;
}

/* If your viewport is short, bring it closer to the edge */
@media (max-height: 640px) {
  .clips-btn {
    bottom: calc(env(safe-area-inset-bottom) + 46px);
  }
}
/* If your Canvas floater sits higher than expected, nudge Clips a bit more */
@media (min-height: 720px) {
  .clips-btn {
    bottom: calc(env(safe-area-inset-bottom) + 46px);
  }
}
/* --- Clippings UI modal --- */
.clips-modal {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.55);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  display: flex;
  align-items: center; /* center vertically */
  justify-content: center; /* center horizontally */
  padding: 4vh 2vw; /* breathing room on tiny screens */
  z-index: 50;
}
.clips-modal.hidden {
  display: none;
}

.clips-panel {
  width: min(900px, 96vw);
  max-height: 92vh; /* allow taller content while staying centered */
  background: var(--panel, #ffffff);
  color: var(--text, #0b1220);
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 16px; /* all corners now (not bottom sheet style) */
  box-shadow: var(--shadow);
  overflow: auto;
}
.clips-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
}
.clips-head h2 {
  font-size: 16px;
  margin: 0;
}
.clips-close {
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 10px;
  cursor: pointer;
}
.clips-close:hover {
  background: rgba(148, 163, 184, 0.12);
}
.clips-body {
  padding: 12px;
}
.clips-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}
.clip-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: 12px;
  background: rgba(148, 163, 184, 0.05);
}
.clip-top {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
}
.clip-title {
  font-weight: 700;
  font-size: 14px;
  line-height: 1.2;
  max-height: 2.4em;
  overflow: hidden;
}
.clip-url {
  font-size: 12px;
  color: var(--muted);
  word-break: break-all;
}

.clip-url,
.clips-empty {
  color: var(--muted, #64748b);
}

/* === Icon containers: center contents perfectly === */
.clip-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}
.clip-action {
  appearance: none;
  display: inline-grid; /* <-- centers children */
  place-items: center; /* <-- perfect centering */
  width: 28px;
  height: 28px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
}
.clip-action:hover {
  background: rgba(148, 163, 184, 0.12);
}
.clip-action svg {
  width: 16px;
  height: 16px;
  display: block;
}
/* Strong, accessible keyboard focus */
.clip-action:focus-visible,
.clips-close:focus-visible {
  outline: 2px solid #60a5fa; /* blue ring */
  outline-offset: 2px;
}

/* Text blocks */
.clip-preview {
  font-size: 13px;
  color: var(--text);
  max-height: 6em;
  overflow: hidden;
}
.clip-full {
  white-space: pre-wrap;
  background: rgba(148, 163, 184, 0.06);
  border: 1px dashed rgba(148, 163, 184, 0.25);
  padding: 10px;
  border-radius: 8px;
}
.clip-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}
.clips-empty {
  text-align: center;
  color: var(--muted);
  padding: 20px;
}

/* === AI edit modal === */
.ai-modal.hidden {
  display: none;
}
.ai-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.38);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 9999;
}
.ai-panel {
  width: min(640px, 92vw);
  max-height: 80vh;
  overflow: auto;
  background: var(--panel, #121318);
  color: var(--text, #e5e7eb);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}
.ai-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.18);
  position: sticky;
  top: 0;
  background: inherit;
  z-index: 1;
}
.ai-head h2 {
  margin: 0;
  font-size: 16px;
}
.ai-close {
  margin-left: auto;
  width: 32px;
  height: 32px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 10px;
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.ai-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ai-label {
  font-size: 12px;
  opacity: 0.8;
}
.ai-source,
.ai-result {
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 10px;
  padding: 8px 10px;
  min-height: 56px;
  background: rgba(148, 163, 184, 0.06);
  white-space: pre-wrap;
}
.ai-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}
.ai-btn {
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: inherit;
  cursor: pointer;
  font-size: 12px;
}
.ai-btn:hover {
  background: rgba(148, 163, 184, 0.12);
}
.ai-custom {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}
.ai-custom input {
  flex: 1;
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: inherit;
}
.ai-custom .primary,
.ai-foot .primary {
  background: rgba(99, 102, 241, 0.15);
  border: 1px solid rgba(99, 102, 241, 0.4);
}
.ai-foot {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  padding-top: 6px;
}
.ai-foot button {
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent; /* no special primary styling */
  color: inherit;
}
.ai-foot button:hover {
  background: rgba(148, 163, 184, 0.12);
}
.ai-progress {
  font-size: 12px;
  opacity: 0.8;
}
.ai-key button {
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: inherit;
  cursor: pointer;
}
/* Icon-only "Run" button */
.ai-icon {
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.ai-icon:hover {
  background: rgba(148, 163, 184, 0.12);
}
.ai-icon svg {
  display: block;
}
/* === Floating AI nub (popover trigger) === */
.ai-nub {
  position: absolute;
  z-index: 10000;
  display: grid; place-items: center;
  width: 24px; height: 24px;        /* used in JS for positioning */
  font-size: 18px; line-height: 1;
  padding: 0;
  border: none;                      /* no border */
  background: transparent;           /* no background */
  box-shadow: none;                  /* no shadow */
  cursor: pointer; user-select: none;
}
.ai-nub:hover {
  background: transparent; transform: none; 
}
.ai-nub.hidden {
  display: none;
}
