:root {
  --bg: #f2f2f7;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #86868b;
  --accent: #4f46e5;
  --border: #e5e5ea;
  --danger: #ff3b30;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --panel: #1c1c1e;
    --text: #f5f5f7;
    --muted: #98989d;
    --accent: #8583f0;
    --border: #2c2c2e;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  -webkit-font-smoothing: antialiased;
}

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

section {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
section[hidden] { display: none; }

header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px 8px;
  padding-top: calc(10px + env(safe-area-inset-top));
}
header h1 {
  font-size: 22px;
  margin: 0 4px;
}
.spacer { flex: 1; }

button.icon {
  width: 40px;
  height: 40px;
  padding: 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}
button.icon svg { width: 100%; height: 100%; display: block; }
button.icon:active { background: var(--border); }
button.icon.danger { color: var(--danger); }
button.icon.active { background: var(--accent); color: #fff; }

button.primary {
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  padding: 14px 28px;
  cursor: pointer;
}
button.primary:disabled { opacity: 0.5; }

button.ghost {
  display: block;
  margin: 16px auto 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  padding: 10px;
  cursor: pointer;
}

/* ---------- ログイン画面 ---------- */

#authView { justify-content: center; }
.auth-box {
  text-align: center;
  padding: 24px;
  max-width: 420px;
  margin: 0 auto;
}
.auth-logo {
  width: 76px;
  height: 76px;
  border-radius: 18px;
}
.auth-box h1 { margin: 12px 0 8px; }
.auth-box p { color: var(--text); line-height: 1.7; }
.auth-box p.note {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.7;
  margin-bottom: 24px;
}
.error { color: var(--danger); font-size: 14px; }

/* ---------- 一覧 ---------- */

#fileList {
  list-style: none;
  margin: 4px 12px;
  padding: 0;
  overflow-y: auto;
  border-radius: 12px;
}
#fileList li {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  padding: 15px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
#fileList li:first-child { border-radius: 12px 12px 0 0; }
#fileList li:last-child { border-radius: 0 0 12px 12px; border-bottom: 0; }
#fileList li:only-child { border-radius: 12px; }
#fileList li:active { background: var(--border); }
.file-name {
  flex: 1;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-date {
  color: var(--muted);
  font-size: 13px;
  flex-shrink: 0;
}
.empty {
  text-align: center;
  color: var(--muted);
  margin-top: 48px;
  line-height: 1.8;
}

/* ---------- エディタ ---------- */

#editorView header { gap: 4px; }
#titleInput {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  padding: 8px 6px;
  border-radius: 8px;
  outline: none;
}
#titleInput:focus { background: var(--panel); }

.statusbar {
  padding: 0 18px 6px;
  min-height: 22px;
}
#saveStatus { color: var(--muted); font-size: 12px; }
#saveStatus.error { color: var(--danger); }

#editor {
  flex: 1;
  min-height: 0;
  border: 0;
  resize: none;
  outline: none;
  margin: 0 12px;
  margin-bottom: calc(12px + env(safe-area-inset-bottom));
  padding: 16px;
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font: 16px/1.8 ui-monospace, "SF Mono", Menlo, monospace;
}

.preview {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0 12px;
  margin-bottom: calc(12px + env(safe-area-inset-bottom));
  padding: 16px 20px;
  border-radius: 12px;
  background: var(--panel);
  line-height: 1.8;
  word-break: break-word;
}
.preview h1, .preview h2, .preview h3 { line-height: 1.4; }
.preview h1 { font-size: 24px; }
.preview h2 { font-size: 20px; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.preview h3 { font-size: 17px; }
.preview code {
  background: var(--bg);
  border-radius: 5px;
  padding: 2px 6px;
  font-size: 14px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.preview pre {
  background: var(--bg);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
}
.preview pre code { background: transparent; padding: 0; }
.preview blockquote {
  margin: 8px 0;
  padding: 2px 14px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}
.preview a { color: var(--accent); }
.preview hr { border: 0; border-top: 1px solid var(--border); }

/* ---------- トースト ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(32px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(30, 30, 32, 0.92);
  color: #fff;
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 22px;
  max-width: 86vw;
  z-index: 10;
}
