:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --sidebar: #f0f0ed;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --faint: #a1a1a6;
  --line: #e2e2de;
  --hover: rgba(0, 0, 0, 0.05);
  --selected: #ffe9a8;
  --accent: #c98a00;
  --accent-strong: #a36f00;
  --accent-text: #ffffff;
  --link: #0a66c2;
  --danger: #d70015;
  --code-bg: #f2f2f0;
  --quote: #c7c7cc;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.18);
  --radius: 10px;
  --vvh: 100dvh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --panel: #1c1c1e;
    --sidebar: #232325;
    --text: #f2f2f7;
    --muted: #98989f;
    --faint: #636366;
    --line: #38383a;
    --hover: rgba(255, 255, 255, 0.06);
    --selected: #4a3b10;
    --accent: #f0b429;
    --accent-strong: #ffc94d;
    --accent-text: #1c1c1e;
    --link: #4da3ff;
    --danger: #ff453a;
    --code-bg: #2c2c2e;
    --quote: #48484a;
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
body { overflow: hidden; overscroll-behavior: none; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ——— Layout ——— */

.app {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
  height: var(--vvh);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  padding-top: var(--safe-top);
}
.sidebar-head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 14px 12px 8px 18px;
}
.sidebar-head h1 { flex: 1; margin: 0; font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 40px;
  height: 40px;
  border-radius: 8px;
  color: var(--accent-strong);
}
.icon-btn:hover { background: var(--hover); }
.icon-btn[aria-pressed="true"] { color: var(--accent); }
.icon-btn[aria-pressed="true"] svg { fill: currentColor; }

.search {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 12px 8px;
  padding: 0 10px;
  height: 36px;
  border-radius: 9px;
  background: var(--hover);
  color: var(--muted);
}
.search svg { width: 17px; height: 17px; flex: none; }
.search input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  outline: none;
}

.note-list {
  list-style: none;
  margin: 0;
  padding: 0 8px 12px;
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}
.list-heading {
  padding: 12px 10px 4px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.note-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 10px 10px;
  border-radius: 8px;
  border-bottom: 1px solid var(--line);
}
.note-item:hover { background: var(--hover); }
.note-item[aria-current="true"] { background: var(--selected); border-bottom-color: transparent; }
.note-item-body { flex: 1; min-width: 0; }
.note-title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.note-meta {
  display: flex;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}
.note-meta > span:first-child { flex: none; color: var(--text); opacity: 0.75; }
.note-preview { overflow: hidden; text-overflow: ellipsis; }
.thumb { width: 44px; height: 44px; border-radius: 6px; object-fit: cover; flex: none; background: var(--hover); }
.badge {
  display: inline-block;
  margin-right: 6px;
  padding: 0 6px;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  vertical-align: 2px;
  background: var(--danger);
  color: #fff;
}
.note-list:empty { display: none; }
.list-empty { flex: 1; padding: 24px; text-align: center; color: var(--muted); }

.sidebar-foot {
  padding: 8px 14px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--line);
}
.sync-status { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; color: var(--muted); }
.sync-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.sync-status[data-state="synced"] .dot { background: #30a46c; }
.sync-status[data-state="syncing"] .dot { background: var(--accent); animation: pulse 1s infinite alternate; }
.sync-status[data-state="error"] .dot, .sync-status[data-state="signed-out"] .dot { background: var(--danger); }
@keyframes pulse { to { opacity: 0.3; } }

.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
  padding-top: var(--safe-top);
}
.editor-head {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 10px 2px;
}
.editor-head .spacer { flex: 1; }
.note-date { font-size: 0.82rem; color: var(--muted); padding-left: 8px; }
.back { display: none; padding-right: 8px; font-size: 1.05rem; }
.back svg { width: 26px; height: 26px; margin-left: -6px; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px 10px 6px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.toolbar button {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 34px;
  padding: 0 4px;
  border-radius: 7px;
  color: var(--muted);
}
.toolbar button svg { width: 20px; height: 20px; }
.toolbar button.txt { font-size: 1rem; font-weight: 600; font-family: Georgia, "Times New Roman", serif; }
.toolbar button.txt:nth-child(n+6) { font-family: inherit; font-size: 0.85rem; }
.toolbar button:hover { background: var(--hover); color: var(--text); }
.toolbar button[aria-pressed="true"] { background: var(--selected); color: var(--text); }
.toolbar .sep { flex: none; width: 1px; height: 20px; margin: 0 4px; background: var(--line); }

.banner {
  padding: 8px 18px;
  font-size: 0.9rem;
  background: var(--selected);
}

.editor-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.editor {
  max-width: 46rem;
  min-height: 100%;
  margin: 0 auto;
  padding: 18px 24px calc(80px + var(--safe-bottom));
  outline: none;
  font-size: 1.05rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
  caret-color: var(--accent);
}
.editor.is-empty::before {
  content: attr(data-placeholder);
  position: absolute;
  color: var(--faint);
  pointer-events: none;
}
.app:not(.has-note) .editor-head > :not(.back),
.app:not(.has-note) .toolbar,
.app:not(.has-note) .editor-scroll { visibility: hidden; }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  pointer-events: none;
}
.empty svg { width: 44px; height: 44px; stroke-width: 1.3; color: var(--faint); }
.empty .hint { font-size: 0.85rem; color: var(--faint); margin: 0; }

/* ——— Note content (editor) ——— */

.editor > :first-child { margin-top: 0; }
.editor p { margin: 0 0 0.35em; }
.editor h1 { font-size: 1.75rem; line-height: 1.2; margin: 0.6em 0 0.3em; letter-spacing: -0.01em; }
.editor h2 { font-size: 1.35rem; line-height: 1.25; margin: 0.8em 0 0.3em; }
.editor h3 { font-size: 1.12rem; margin: 0.8em 0 0.25em; }
.editor a { color: var(--link); }
.editor ul, .editor ol { margin: 0.2em 0 0.5em; padding-left: 1.6em; }
.editor li { margin: 0.1em 0; }
.editor blockquote { margin: 0.5em 0; padding: 0.05em 0 0.05em 1em; border-left: 3px solid var(--quote); color: var(--muted); }
.editor pre {
  margin: 0.5em 0;
  padding: 0.7em 0.9em;
  border-radius: 8px;
  background: var(--code-bg);
  font: 0.9rem/1.45 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre-wrap;
}
.editor code { font: 0.9em ui-monospace, "SF Mono", Menlo, Consolas, monospace; background: var(--code-bg); padding: 0.1em 0.3em; border-radius: 4px; }
.editor pre code { background: none; padding: 0; }
.editor hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }

.editor ul[data-type="checklist"] { list-style: none; padding-left: 1.9em; }
.editor ul[data-type="checklist"] > li { position: relative; }
.editor ul[data-type="checklist"] > li::before {
  content: "";
  position: absolute;
  left: -1.75em;
  top: 0.18em;
  width: 1.1em;
  height: 1.1em;
  border: 1.6px solid var(--faint);
  border-radius: 50%;
  cursor: pointer;
}
.editor ul[data-type="checklist"] > li[data-checked="true"]::before {
  border-color: var(--accent);
  background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 8.5l2.5 2.5L12 5.5' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/80% no-repeat;
}
.editor ul[data-type="checklist"] > li[data-checked="true"] { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--faint); }

.editor img[data-att] {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0.4em 0;
  border-radius: 8px;
  cursor: zoom-in;
  background: var(--code-bg);
}
.editor img.loading, .editor img.missing { min-height: 120px; width: 100%; max-width: 360px; object-fit: contain; }
.editor img.missing { outline: 1px dashed var(--faint); }
.editor .att, .att {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  margin: 0.1em 0;
  padding: 0.2em 0.65em 0.2em 0.5em;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--code-bg);
  font-size: 0.92em;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  vertical-align: baseline;
}
.editor .att::before { content: "📎"; }

/* ——— Sign-in ——— */

.auth {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}
.auth-card { width: 100%; max-width: 360px; text-align: center; }
.auth-card h1 { margin: 12px 0 4px; font-size: 1.8rem; }
.auth-msg { color: var(--muted); margin: 0 0 20px; }
.auth-error { color: var(--danger); min-height: 1.5em; }
.setup-form { display: grid; gap: 12px; text-align: left; margin-bottom: 8px; }
.setup-form label, dialog label { display: grid; gap: 4px; font-size: 0.9rem; color: var(--muted); }
input:not([type="checkbox"]):not([type="search"]) {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* stops iOS zooming in */
}

.primary, .secondary, .danger {
  display: block;
  width: 100%;
  height: 46px;
  border-radius: 11px;
  font-weight: 600;
}
.primary { background: var(--accent); color: var(--accent-text); }
.primary:hover { background: var(--accent-strong); }
.secondary { background: var(--hover); color: var(--text); }
.danger { background: transparent; color: var(--danger); border: 1px solid var(--line); }
.link-btn { color: var(--accent-strong); font-weight: 600; padding: 6px 4px; }
.danger-text { color: var(--danger); }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }

/* ——— Dialogs ——— */

dialog {
  border: 0;
  padding: 0;
  color: var(--text);
  background: var(--panel);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog form { padding: 16px 20px 20px; }
dialog header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
dialog header h2 { flex: 1; margin: 0; font-size: 1.2rem; }
dialog h3 { margin: 18px 0 6px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
dialog .secondary, dialog .danger, dialog .primary { margin-top: 10px; }

.sheet { width: min(440px, calc(100vw - 24px)); border-radius: 16px; max-height: calc(var(--vvh) - 40px); }
.alert { width: min(360px, calc(100vw - 32px)); border-radius: 14px; }
.alert .actions { display: flex; gap: 10px; margin-top: 16px; }
.alert .actions button { margin: 0; }

.format-list { display: grid; gap: 6px; }
.format { text-align: left; padding: 12px 14px; border-radius: 10px; background: var(--hover); font-weight: 500; }
.format:hover { background: var(--selected); }

.passkey-list { list-style: none; margin: 0; padding: 0; }
.passkey-list li { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.row { display: flex !important; justify-content: space-between; align-items: center; color: var(--text) !important; font-size: 1rem !important; }
.row input { width: 22px; height: 22px; accent-color: var(--accent); }

.viewer { width: min(960px, 100vw); max-width: 100vw; height: min(90vh, var(--vvh)); border-radius: 14px; }
.viewer form { display: flex; flex-direction: column; height: 100%; padding: 10px 14px calc(14px + var(--safe-bottom)); }
.viewer header { padding-top: var(--safe-top); }
.viewer header .spacer { flex: 1; }
#viewer-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-body { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.viewer-body img, .viewer-body video { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.file-card { text-align: center; }
.file-card svg { width: 64px; height: 64px; stroke-width: 1.2; }

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  border: 3px dashed var(--accent);
  pointer-events: none;
}
.drop-overlay div { padding: 14px 22px; border-radius: 12px; background: var(--panel); box-shadow: var(--shadow); font-weight: 600; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: #2c2c2e;
  color: #fff;
  box-shadow: var(--shadow);
  font-size: 0.92rem;
}
.toast button { color: #ffc94d; font-weight: 600; }

.print-frame { position: fixed; right: 0; bottom: 0; width: 1px; height: 1px; border: 0; opacity: 0; }

/* ——— Phones: one pane at a time ——— */

@media (max-width: 759px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-right: 0; }
  .main { display: none; }
  .app.has-note .sidebar { display: none; }
  .app.has-note .main { display: flex; }
  .back { display: inline-flex; }
  .note-date { display: none; }
  .editor { padding: 12px 18px calc(80px + var(--safe-bottom)); font-size: 1.06rem; }
  .sheet { margin-bottom: 0; width: 100vw; max-width: 100vw; border-radius: 16px 16px 0 0; padding-bottom: var(--safe-bottom); }
  .viewer { width: 100vw; height: var(--vvh); max-height: none; border-radius: 0; }
}
