:root {
  color-scheme: light dark;
  --accent: #3a6ea5;
  --bg: #ffffff;
  --fg: #1a202c;
  --border: #e2e8f0;
  --muted: #718096;
  --content-bg: #ffffff;
  --titlebar-bg: #333333;
  --titlebar-fg: #ffffff;
  --toolbar-bg: #555555;
  --toolbar-fg: #eeeeee;
  --editor-bg: #ffffee;
  --note-bg: #ffffcc;
  --note-fg: #666644;
  --code-bg: #f4f4f4;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--fg);
}

#app {
  max-width: 720px;
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- title bar + toolbar (w2wiki look) ---- */

.titlebar {
  background: var(--titlebar-bg);
  color: var(--titlebar-fg);
  font-size: 1.15rem;
  font-weight: bold;
  padding: calc(6px + env(safe-area-inset-top)) 12px 6px;
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-shrink: 0;
}

.titlebar-back {
  align-self: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px 0 0;
  margin: 0;
}

.titlebar-back:hover {
  opacity: 0.7;
}

.titlebar-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.titledate {
  font-size: 0.7rem;
  font-weight: normal;
  color: #bbbbbb;
}

.toolbar {
  background: var(--toolbar-bg);
  padding: 6px 12px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  flex-shrink: 0;
}

a.tool,
.tool {
  color: var(--toolbar-fg);
  font-size: 0.8rem;
  text-decoration: none;
  cursor: pointer;
}

a.tool:hover {
  text-decoration: underline;
}

.tool.status {
  color: #cccccc;
  font-style: italic;
}

.search-form {
  margin: 0;
  margin-left: auto;
}

.search-input {
  font-size: 0.8rem;
  padding: 3px 6px;
  border-radius: 4px;
  border: 1px solid #00000033;
  width: 8rem;
  color: #000;
  background: #fff;
}

/* ---- content ---- */

.main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px calc(24px + env(safe-area-inset-bottom));
}

.wiki-content {
  line-height: 1.55;
}

.wiki-content h1 { font-size: 1.5rem; margin: 0.6em 0 0.4em; }
.wiki-content h2 { font-size: 1.25rem; margin: 0.6em 0 0.4em; }
.wiki-content h3 { font-size: 1.05rem; margin: 0.6em 0 0.4em; }
.wiki-content p { margin: 0.7em 0; }
.wiki-content ul, .wiki-content ol { margin: 0.7em 0; padding-left: 1.6em; }
.wiki-content li { line-height: 1.5; }
.wiki-content hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }

.wiki-content blockquote {
  margin: 0.7em 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border);
  background: var(--code-bg);
}

.wiki-content pre {
  background: var(--code-bg);
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.wiki-content code,
.wiki-content pre {
  font-family: Menlo, ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace;
  font-size: 0.85em;
}

.wiki-content :not(pre) > code {
  background: var(--code-bg);
  padding: 1px 4px;
  border-radius: 4px;
}

.wiki-content img.wiki-image {
  max-width: 100%;
  height: auto;
}

.wiki-content .wiki-image-placeholder {
  display: inline-block;
  padding: 2px 8px;
  border: 1px dashed var(--border);
  border-radius: 4px;
  color: var(--muted);
  font-size: 0.85em;
}

a.wikilink { color: var(--accent); text-decoration: none; }
a.wikilink:hover { text-decoration: underline; }
a.redlink { color: #c0392b; }

/* ---- editor ---- */

.edit-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}

.title-input {
  width: 100%;
  font-size: 1.1rem;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
  padding: 8px;
  margin-bottom: 8px;
  background: var(--bg);
  color: var(--fg);
}

.content-input {
  flex: 1;
  width: 100%;
  min-height: 40vh;
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
  resize: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  font-size: 0.9rem;
  line-height: 1.5;
  padding: 10px;
  background: var(--editor-bg);
  color: #1a202c;
  font-family: Menlo, ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace;
}

/* ---- page list (All / Recent) ---- */

.page-list {
  width: 100%;
  border-collapse: collapse;
}

.page-list td {
  padding: 7px 6px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.page-list a { color: var(--accent); text-decoration: none; }
.page-list a:hover { text-decoration: underline; }
.page-list .col-date { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.page-list .col-edit { text-align: right; font-size: 0.8rem; }

/* ---- search ---- */

.search-results { list-style: none; padding: 0; margin: 0.5em 0; }
.search-results li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.search-results a { color: var(--accent); text-decoration: none; }
.note-count { color: var(--muted); font-size: 0.85rem; }

/* ---- shared bits ---- */

.note {
  background: var(--note-bg);
  padding: 8px 10px;
  border-radius: 4px;
  color: var(--note-fg);
  border: 1px solid var(--note-fg);
}

.tool-link { color: var(--accent); }

.badge {
  font-size: 0.65rem;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--border);
  color: var(--muted);
}
.badge-pending { background: #feebc8; color: #7b341e; }
.badge-error { background: #fed7d7; color: #822727; }

.empty-state {
  color: var(--muted);
  text-align: center;
  padding: 30px 0;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

/* ---- settings ---- */

.settings-form { display: flex; flex-direction: column; gap: 14px; }
.settings-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.settings-form input {
  padding: 9px 11px;
  border-radius: 6px;
  border: 1px solid var(--border);
  font-size: 0.95rem;
  color: var(--fg);
  background: var(--bg);
}
.password-row { display: flex; gap: 6px; align-items: center; }
.password-row input { flex: 1; }
.help-text { font-size: 0.78rem; color: var(--muted); line-height: 1.45; }
.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row button,
.sync-panel button {
  padding: 9px 14px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-size: 0.9rem;
  cursor: pointer;
}
#test-btn, #seed-btn { background: none; color: var(--accent); }
.test-result { font-size: 0.85rem; min-height: 1.2em; }
.test-result.success { color: #2f855a; }
.test-result.error { color: #c53030; }
.sync-panel {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-start;
}

/* ---- sync status icon (title bar) ---- */

.sync-status {
  display: inline-flex;
  align-items: center;
  align-self: center;
  /* Push to the right edge of the flex title bar, past the name/date. */
  margin-left: auto;
}

.sync-status:empty {
  display: none;
}

.sync-icon {
  display: block;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a202c;
    --fg: #f7fafc;
    --border: #2d3748;
    --muted: #a0aec0;
    --accent: #63b3ed;
    --content-bg: #1a202c;
    --titlebar-bg: #000000;
    --toolbar-bg: #2d3748;
    --toolbar-fg: #e2e8f0;
    --editor-bg: #202836;
    --note-bg: #3a3520;
    --note-fg: #e6d98a;
    --code-bg: #2d3748;
  }
  .content-input { color: var(--fg); }
  .search-input { color: #000; }
}
