:root {
  --bg: #ffffff;
  --bg-alt: #f2f2f7;
  --fg: #1c1c1e;
  --fg-dim: #6e6e73;
  --border: #d1d1d6;
  --accent: #0a84ff;
  --danger: #ff453a;
  --row-alt: #f7f7f9;
  --row-hover: #e5e5ea;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --bg-alt: #2c2c2e;
    --fg: #f2f2f7;
    --fg-dim: #98989d;
    --border: #3a3a3c;
    --row-alt: #242426;
    --row-hover: #3a3a3c;
  }
}

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

html, body { height: 100%; }

body {
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
}

.hidden { display: none !important; }

button {
  font: inherit;
  color: var(--fg);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 10px;
  cursor: pointer;
}
button:disabled { opacity: 0.4; cursor: default; }

input, textarea {
  font: inherit;
  color: var(--fg);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.tag-chips { display: flex; flex-wrap: wrap; gap: 6px; min-height: 0; margin: 4px 0 8px; }
.tag-chips:not(:empty) { min-height: 32px; }
.tag-chip { padding: 4px 8px; border-radius: 999px; color: var(--fg); background: var(--bg-alt); }
.tag-chip:hover { border-color: var(--danger); color: var(--danger); }
.tag-suggestion { padding: 5px 10px; border-radius: 999px; }
.tag-suggestion:disabled { opacity: 0.35; }

/* Login */
#login {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(360px, 100%);
}
#login-form h1 { font-size: 22px; }
#login-form p { color: var(--fg-dim); font-size: 13px; }
#login-form button { background: var(--accent); border-color: var(--accent); color: #fff; }
.error { color: var(--danger); font-size: 13px; }

/* App layout */
#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  gap: 4px;
  padding: 6px;
  border-bottom: 1px solid var(--border);
}
header #search-controls { flex: 1; position: relative; min-width: 0; }
header #search { width: 100%; padding-right: 40px; }
header #clear-search {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--accent);
  font-size: 17px;
  line-height: 1;
  touch-action: manipulation;
}
header #random-note { width: 32px; padding-inline: 0; font-size: 18px; }
header #new-note { width: 32px; padding: 2px 0; font-size: 20px; line-height: 1; }
header #info-button { width: 32px; padding-inline: 0; font-size: 17px; font-weight: 600; }

#note-list {
  flex: 0 1 216px;
  max-height: 216px;
  min-height: 0;
  overflow-y: auto;
  list-style: none;
}
#note-list li {
  display: flex;
  gap: 7px;
  align-items: baseline;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
#note-list li:nth-child(even) { background: var(--row-alt); }
#note-list li:hover { background: var(--row-hover); }
#note-list li.selected { background: var(--accent); color: #fff; }
#note-list li .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#note-list li .list-tags {
  max-width: 30%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 10px;
  color: var(--fg-dim);
}
#note-list li .date { font-size: 11px; color: var(--fg-dim); flex-shrink: 0; }
#note-list li.selected .date,
#note-list li.selected .list-tags { color: rgba(255, 255, 255, 0.75); }

#editor-pane {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
#editor {
  flex: 1;
  border: none;
  border-radius: 0;
  resize: none;
  padding: 10px;
  background: var(--bg);
}
#preview {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  line-height: 1.5;
  word-break: break-word;
}
#preview h1 { font-size: 20px; margin: 2px 0 10px; }
#preview h2 { font-size: 17px; margin: 14px 0 6px; }
#preview h3, #preview h4, #preview h5, #preview h6 { font-size: 15px; margin: 12px 0 4px; }
#preview p { margin: 0 0 10px; }
#preview ul, #preview ol { margin: 0 0 10px; padding-left: 22px; }
#preview li { margin: 2px 0; }
#preview a { color: var(--accent); }
#preview blockquote {
  margin: 0 0 10px;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--fg-dim);
}
#preview code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  background: var(--bg-alt);
  border-radius: 4px;
  padding: 1px 4px;
}
#preview pre {
  margin: 0 0 10px;
  padding: 10px;
  background: var(--bg-alt);
  border-radius: 6px;
  overflow-x: auto;
}
#preview pre code { background: transparent; padding: 0; }
#preview hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
#editor-pane footer {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 8px calc(5px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
#note-meta {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 1px 7px;
  font-size: 11px;
  color: var(--fg-dim);
}
#editor-tags {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#save-status { font-size: 11px; color: var(--fg-dim); white-space: nowrap; }
#delete-note { color: var(--danger); }
#save-note { background: var(--accent); border-color: var(--accent); color: #fff; }
#save-note:disabled {
  opacity: 1;
  color: var(--fg-dim);
  background: var(--bg-alt);
  border-color: var(--border);
}

dialog {
  width: min(420px, calc(100% - 32px));
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px);
  margin: auto;
  padding: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
#tag-form, #date-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px;
}
#tag-form h2, #date-form h2 { font-size: 18px; }
#tag-form label, #date-form label { font-size: 13px; }
#tag-form p { font-size: 12px; color: var(--fg-dim); }
#info-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px;
}
#info-form h2 { font-size: 18px; }
#info-details {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.6fr);
  gap: 7px 14px;
  font-size: 13px;
}
#info-details dt { color: var(--fg-dim); }
#info-details dd { overflow-wrap: anywhere; }
.info-note { font-size: 12px; color: var(--fg-dim); }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}
.dialog-actions .primary { background: var(--accent); border-color: var(--accent); color: #fff; }

@media (max-width: 520px) {
  dialog {
    position: fixed;
    left: 50%;
    margin: 0;
    transform: translateX(-50%);
  }
  #editor-pane footer { flex-wrap: nowrap; }
  #editor-pane footer button { padding-inline: 9px; }
  #note-meta { display: none; }
}
