:root {
  --bg: #ffffff;
  --bg-muted: #fafafa;
  --fg: #0a0a0a;
  --muted: #71717a;
  --border: #e4e4e7;
  --accent: #18181b;
  --accent-fg: #fafafa;
  --danger: #dc2626;
  --hover: #f4f4f5;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --bg-muted: #18181b;
    --fg: #fafafa;
    --muted: #a1a1aa;
    --border: #27272a;
    --accent: #fafafa;
    --accent-fg: #09090b;
    --hover: #18181b;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg-muted); color: var(--fg); font-family: var(--font); font-size: 15px; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; cursor: pointer; background: transparent; border: none; }
input, textarea { font: inherit; color: inherit; }

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 4px;
  padding: 0 8px; height: 52px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.title { flex: 1; font-weight: 600; font-size: 16px; padding: 0 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 8px; border-radius: var(--radius);
  min-width: 40px; min-height: 40px;
  color: var(--fg);
}
.icon-btn:hover, .icon-btn:active { background: var(--hover); }
.icon-btn { position: relative; }
.icon-btn .dot {
  position: absolute; top: 6px; right: 6px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--danger); display: none;
  border: 2px solid var(--bg);
  box-sizing: content-box;
}
#save-btn { color: var(--muted); }
#save-btn.dirty { color: var(--fg); }
#save-btn.dirty .dot { display: block; }
.icon { width: 20px; height: 20px; display: block; }

.main { flex: 1; display: flex; flex-direction: column; }
.screen { display: none; flex: 1; flex-direction: column; padding: 8px 16px 80px; }
.screen.active { display: flex; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { margin: 0; }
.list .row {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 4px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.list li:last-child .row { border-bottom: none; }
.list .row:active { background: var(--hover); }
.list .row .icon { color: var(--muted); flex-shrink: 0; }
.list .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }

.empty { color: var(--muted); text-align: center; margin-top: 80px; font-size: 13px; }

.file-name {
  width: 100%; border: none; outline: none;
  font-size: 20px; font-weight: 600;
  padding: 8px 0 12px;
  background: transparent;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
#editor, .viewer {
  width: 100%; flex: 1; min-height: 60vh;
  border: none; outline: none; resize: none;
  padding: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px; line-height: 1.7;
  background: transparent;
  color: var(--fg);
}
.viewer {
  white-space: pre-wrap;
  word-break: break-word;
  cursor: text;
}
.viewer:empty::before {
  content: "ここに書く...";
  color: var(--muted);
}
.viewer a {
  color: #2563eb;
  text-decoration: underline;
  word-break: break-all;
}
@media (prefers-color-scheme: dark) {
  .viewer a { color: #60a5fa; }
}

.auth { width: 100%; padding: 48px 4px; display: flex; flex-direction: column; gap: 10px; }
.auth-title { font-size: 20px; font-weight: 600; margin: 0 0 8px; }
.auth input[type="email"], .auth input[type="password"], .auth input[type="text"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--fg); font: inherit; font-size: 16px; outline: none;
}
.auth input:focus { border-color: var(--accent); }
.auth [hidden] { display: none !important; }
.auth-check { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.auth-error { color: var(--danger); font-size: 13px; margin: 0; min-height: 0; }
.auth-info { color: var(--muted); font-size: 13px; margin: 0; }
.auth-error:empty, .auth-info:empty { display: none; }
.auth-links { display: flex; justify-content: space-between; margin-top: 6px; }
.link { background: none; border: none; color: var(--muted); font: inherit; font-size: 13px; cursor: pointer; padding: 4px 0; text-decoration: underline; }
.btn:disabled { opacity: .5; cursor: default; }

.btn {
  padding: 10px 16px;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: var(--radius);
  font-size: 14px;
}
.btn:active { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.block { width: 100%; }

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 300;
  display: none;
  align-items: flex-end;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--bg);
  width: 100%; max-width: 480px;
  margin: 0 auto;
  border-radius: 16px 16px 0 0;
  padding: 12px 16px max(20px, env(safe-area-inset-bottom));
  animation: slideUp .22s ease;
  border-top: 1px solid var(--border);
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }
.modal-handle {
  width: 36px; height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 14px;
}
.modal-title { font-weight: 600; font-size: 16px; margin-bottom: 8px; }
.modal-body { font-size: 14px; color: var(--muted); margin-bottom: 14px; line-height: 1.5; }
.modal-body:empty { display: none; }
.modal-input-wrap { margin-bottom: 14px; }
.modal-input-wrap input {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius);
  outline: none; background: var(--bg);
}
.modal-input-wrap input:focus { border-color: var(--accent); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions .btn { min-width: 80px; }

.sheet-item {
  display: block; width: 100%;
  padding: 14px 12px;
  text-align: left;
  font-size: 15px;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
}
.sheet-item:last-child { border-bottom: none; }
.sheet-item:active { background: var(--hover); }
.sheet-item.danger { color: var(--danger); }
.sheet-item.cancel { color: var(--muted); margin-top: 8px; text-align: center; border-top: 1px solid var(--border); border-bottom: none; }

.toast {
  position: fixed; left: 50%; bottom: 24px;
  transform: translateX(-50%);
  background: var(--accent); color: var(--accent-fg);
  padding: 10px 16px; border-radius: 999px;
  font-size: 13px; z-index: 400;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translateX(-50%); } }
