:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --border: #e0e0e0;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --warn-bg: #fff7e6;
  --warn-border: #f4c542;
  --editing-bg: #f0fdf4;
  --editing-border: #34a853;
}

/* System dark mode — unless the user forced light via the 🌙/☀️ toggle. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #14161a;
    --fg: #eaeaea;
    --muted: #9a9a9a;
    --border: #2b2e34;
    --accent: #5b8dfd;
    --accent-fg: #0b0d10;
    --warn-bg: #2a2410;
    --warn-border: #8a6d1a;
    --editing-bg: #0f1f14;
    --editing-border: #2f8f4e;
  }
}

/* Forced dark via the toggle (same values as the media query above). */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14161a;
  --fg: #eaeaea;
  --muted: #9a9a9a;
  --border: #2b2e34;
  --accent: #5b8dfd;
  --accent-fg: #0b0d10;
  --warn-bg: #2a2410;
  --warn-border: #8a6d1a;
  --editing-bg: #0f1f14;
  --editing-border: #2f8f4e;
}

:root[data-theme="light"] {
  color-scheme: light;
}

* { box-sizing: border-box; }

.hidden { display: none; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  /* dvh tracks the *visible* viewport on mobile (URL bar, keyboard) */
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
}

.statusline {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex: 1;
  min-width: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.status {
  flex: 1;
  min-width: 0;
}

.counts {
  white-space: nowrap;
}

/* Full-width row under the buttons (the topbar wraps): editable note
   title on the left, save/expiry meta on the right. */
.metabar {
  order: 5;
  flex-basis: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.title-input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  outline: none;
  color: var(--fg);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 2px 0;
}
.title-input:disabled {
  color: var(--fg);
  -webkit-text-fill-color: var(--fg);
  opacity: 1;
  cursor: default;
}
.title-input:disabled::placeholder {
  color: transparent; /* quiet when you can't edit it anyway */
}

.updated {
  margin-left: auto;
  text-align: right;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.hist-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.85rem;
}
.hist-info { min-width: 0; }
.hist-preview {
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 280px;
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.name-input {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.85rem;
  width: 160px;
}

.btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 0.85rem;
  cursor: pointer;
}

.btn:hover:not(:disabled) { border-color: var(--accent); }

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-icon {
  padding: 6px 8px;
  line-height: 1;
}

/* Zen mode: hide all chrome, leave only the note (and its gutter). */
body.zen .topbar,
body.zen .lock-banner,
body.zen .pc-pop {
  display: none;
}

.zen-exit {
  position: fixed;
  top: 10px;
  right: 12px;
  z-index: 15;
  opacity: 0.45;
  background: var(--bg);
}
.zen-exit:hover { opacity: 1; }
body:not(.zen) .zen-exit { display: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}

.lock-banner {
  padding: 8px 16px;
  font-size: 0.85rem;
  background: var(--warn-bg);
  border-bottom: 1px solid var(--warn-border);
}

.lock-banner.editing-mine {
  background: var(--editing-bg);
  border-bottom-color: var(--editing-border);
}


.editor-wrap {
  flex: 1;
  display: flex;
  min-height: 0;
}

.gutter {
  overflow: hidden;
  padding: 24px 8px 24px 0;
  text-align: right;
  font-size: 1rem;
  line-height: 1.6;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--muted);
  background: var(--bg);
  border-right: 1px solid var(--border);
  user-select: none;
  flex: none;
}

.gutter-line {
  padding-right: 8px;
  opacity: 0.7;
  font-size: 0.8em;
  line-height: 2; /* 0.8em × 2 = the editor's 1em × 1.6 row height */
  cursor: pointer;
}

.gutter-line:hover {
  opacity: 1;
  color: var(--accent);
}

/* Hidden twin of the editor used to measure how tall each logical line
   renders once wrapped; must share the editor's wrapping behavior. */
.gutter-mirror {
  position: absolute;
  top: 0;
  left: -99999px;
  visibility: hidden;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  tab-size: 4;
}

.editor {
  flex: 1;
  min-width: 0;
  border: none;
  resize: none;
  outline: none;
  padding: 24px;
  font-size: 1rem;
  line-height: 1.6;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  tab-size: 4;
  background: var(--bg);
  color: var(--fg);
}

.editor[readonly] {
  color: var(--muted);
  cursor: default;
}

/* Clickable view mode: the exact same character grid as the editor —
   identical font, padding, line-height and wrapping — with links and
   light color on top. Nothing may change character widths. */
.viewer {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 24px;
  font-size: 1rem;
  line-height: 1.6;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  tab-size: 4;
  background: var(--bg);
  color: var(--fg);
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

body:not(.viewing) .viewer { display: none; }
body.viewing .editor,
body.viewing .gutter { display: none; }

.v-link {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.v-link:hover { opacity: 0.8; }

.v-h1 { color: var(--accent); font-weight: 700; }
.v-h2 { font-weight: 700; }
.v-h3 { font-weight: 600; }
.v-quote { color: var(--muted); font-style: italic; }
.v-fence, .v-pre { color: var(--muted); }

@media (max-width: 600px) {
  .viewer { padding: 16px 12px; }
}

.pc-pop {
  position: absolute;
  top: 52px;
  right: 16px;
  z-index: 10;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  font-size: 0.85rem;
  color: var(--muted);
}

.pc-pop-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.pc-pop label {
  display: block;
  max-width: 250px;
}

.pc-pop input {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 16px;
  width: 90px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

#emailInput,
#acctEmail {
  width: 100%;
  font-family: inherit;
}

.btn-danger {
  border-color: #d33;
  color: #d33;
}
.btn-danger:hover:not(:disabled) { border-color: #d33; background: rgba(221, 51, 51, 0.08); }

.del-warn {
  max-width: 240px;
  color: var(--fg);
  margin-top: 8px;
}

.qr-img {
  display: block;
  width: 180px;
  height: 180px;
  margin: 8px 0;
  image-rendering: pixelated;
  border-radius: 6px;
  background: #fff; /* QR quiet zone must stay light in dark mode too */
}

.qr-url {
  max-width: 200px;
  font-size: 0.75rem;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  overflow-wrap: break-word;
  margin-bottom: 4px;
}

.acct-msg {
  color: #d33;
  font-size: 0.8rem;
  min-height: 1em;
  margin-top: 6px;
  max-width: 250px;
}

.acct-who {
  font-weight: 600;
  color: var(--fg);
  max-width: 250px;
  overflow-wrap: break-word;
}

.my-notes-card {
  max-width: min(480px, 90vw);
  width: 100%;
  align-items: stretch;
  max-height: 70vh;
}

.my-notes-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.my-notes-list a {
  color: var(--fg);
  text-decoration: none;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.my-notes-list a:hover { border-color: var(--accent); }

.my-notes-list .note-id {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  margin-right: 8px;
}

.gate {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.gate.hidden { display: none; }

.gate-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  padding: 32px;
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 320px;
}

.gate-title {
  font-weight: 700;
  font-size: 1.1rem;
}

.gate-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.gate-input {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 1.4rem;
  width: 8ch;
  text-align: center;
  letter-spacing: 0.3em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.gate-msg {
  color: #d33;
  font-size: 0.85rem;
  min-height: 1.2em;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 0.85rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  transition: opacity 0.2s ease;
}

.toast.toast-hidden {
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 600px) {
  .topbar {
    gap: 8px;
    padding: 8px 12px;
  }

  /* Brand and actions share the first row; status gets its own full row
     instead of being squeezed into a skinny word-wrapped column. */
  .actions {
    margin-left: auto;
    min-width: 0;
    flex-wrap: wrap; /* the controls no longer fit one 375px row */
    justify-content: flex-end;
  }

  .btn {
    padding: 6px 10px;
  }
  .statusline {
    order: 3;
    flex-basis: 100%;
  }

  /* Keep the title+timestamp row directly under the buttons, above the
     status row. */
  .metabar {
    order: 2;
  }

  .name-input {
    width: 110px;
    font-size: 16px; /* <16px makes iOS auto-zoom on focus */
  }

  .editor {
    padding: 16px 12px;
  }

  .gutter {
    padding-top: 16px;
    padding-bottom: 16px;
  }

  .pc-pop {
    right: 12px;
  }
}
