/* ============================================
 * Garden Write — Ulysses風マークダウンエディタ
 * ============================================ */

/* --- CSS変数 --- */
:root {
  --bg: #fafafa;
  --fg: #1a1a1a;
  --muted: #8a8a8a;
  --border: #e0e0e0;
  --sidebar-bg: #f0f0f0;
  --sidebar-hover: #e4e4e4;
  --sidebar-active: #d8d8d8;
  --input-bg: #fff;
  --accent: #4a90d9;
  --accent-hover: #3a7bc8;
  --toolbar-bg: #f5f5f5;
  --error: #d44;
  --success: #4a8;
  --radius: 4px;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'Hiragino Kaku Gothic ProN', 'Yu Gothic', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  --font-editor: 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', Georgia, serif;
  --transition: 0.15s ease;
}

[data-theme="dark"] {
  --bg: #1a1a1a;
  --fg: #e0e0e0;
  --muted: #777;
  --border: #333;
  --sidebar-bg: #151515;
  --sidebar-hover: #222;
  --sidebar-active: #2a2a2a;
  --input-bg: #222;
  --toolbar-bg: #222;
}

/* --- リセット＆ベース --- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  height: 100dvh;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
}

input {
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  outline: none;
}

/* ============================================
 * ログイン画面
 * ============================================ */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  background: var(--bg);
}

.login-box {
  text-align: center;
  width: 280px;
}

.login-title {
  font-size: 28px;
  font-weight: 300;
  letter-spacing: 0.08em;
  margin-bottom: 32px;
  color: var(--fg);
}

.login-error {
  color: var(--error);
  font-size: 13px;
  margin-bottom: 16px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-input {
  width: 100%;
  padding: 12px 16px;
  font-size: 18px;
  text-align: center;
  letter-spacing: 0.3em;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--fg);
  transition: border-color var(--transition);
}

.login-input:focus {
  border-color: var(--accent);
  outline: none;
}

.login-btn {
  padding: 10px;
  font-size: 14px;
  font-weight: 500;
  background: var(--fg);
  color: var(--bg);
  border-radius: var(--radius);
  transition: opacity var(--transition);
}

.login-btn:hover {
  opacity: 0.85;
}

.login-remember {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

.login-remember input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ============================================
 * アプリレイアウト
 * ============================================ */
#app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* --- サイドバー --- */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition), opacity var(--transition);
  z-index: 10;
}

.sidebar.hidden {
  transform: translateX(-100%);
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.sidebar-btn {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  border-radius: var(--radius);
  transition: background var(--transition);
}

.sidebar-btn:hover {
  background: var(--sidebar-hover);
}

/* フォルダツリー */
.folder-tree {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  overflow-y: auto;
  max-height: 180px;
}

.folder-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: background var(--transition);
  user-select: none;
}

.folder-item:hover {
  background: var(--sidebar-hover);
}

.folder-item.active {
  background: var(--sidebar-active);
  font-weight: 600;
}

.folder-item .folder-icon {
  font-size: 14px;
  opacity: 0.6;
}

.folder-item .folder-count {
  margin-left: auto;
  font-size: 11px;
  color: var(--muted);
}

/* 投稿リスト */
.post-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.post-item {
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
}

.post-item:hover {
  background: var(--sidebar-hover);
}

.post-item.active {
  background: var(--sidebar-active);
}

.post-item-title {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.post-item-excerpt {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
}

.post-item-date {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

/* サイドバーフッター */
.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
}

.sidebar-btn-sm {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  border-radius: var(--radius);
  transition: background var(--transition);
}

.sidebar-btn-sm:hover {
  background: var(--sidebar-hover);
}

/* ============================================
 * エディタエリア
 * ============================================ */
.editor-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

/* ヘッダー */
.editor-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 48px;
}

.sidebar-toggle {
  font-size: 18px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  transition: background var(--transition);
}

.sidebar-toggle:hover {
  background: var(--sidebar-hover);
}

.editor-meta {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.title-input {
  flex: 1;
  font-size: 14px;
  min-width: 0;
  padding: 4px 0;
}

.title-input::placeholder {
  color: var(--muted);
}

.meta-date {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.meta-status {
  font-size: 11px;
  color: var(--muted);
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  white-space: nowrap;
}

.meta-status.published {
  color: var(--success);
  border-color: var(--success);
}

.editor-actions {
  display: flex;
  gap: 8px;
}

.btn-publish {
  padding: 6px 16px;
  font-size: 13px;
  font-weight: 500;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  transition: background var(--transition);
}

.btn-publish:hover {
  background: var(--accent-hover);
}

/* タブ */
.editor-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
}

.tab {
  padding: 8px 16px;
  font-size: 13px;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  transition: color var(--transition), border-color var(--transition);
}

.tab:hover {
  color: var(--fg);
}

.tab.active {
  color: var(--fg);
  border-bottom-color: var(--fg);
}

/* エディタスクロール（タイプライターモード） */
.editor-scroll {
  flex: 1;
  overflow-y: auto;
  position: relative;
}

/* タイプライターモード:
   テキストエリアの表示幅はそのまま。
   JSがカーソル行を画面の縦中央にスクロールする。 */

.editor-textarea {
  width: 100%;
  min-height: 100%;
  padding: 32px 48px;
  padding-bottom: 50vh; /* カーソルが最下行でも中央に来るよう余白 */
  font-family: var(--font-editor);
  font-size: 16px;
  line-height: 1.9;
  letter-spacing: 0.02em;
  color: var(--fg);
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  -webkit-text-size-adjust: none;
}

.editor-textarea::placeholder {
  color: var(--muted);
}

/* プレビュー */
.preview-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 40px;
  font-family: var(--font-editor);
  font-size: 16px;
  line-height: 1.9;
  letter-spacing: 0.02em;
}

.preview-area h1 { font-size: 24px; font-weight: 700; margin: 24px 0 12px; }
.preview-area h2 { font-size: 20px; font-weight: 700; margin: 20px 0 10px; }
.preview-area h3 { font-size: 17px; font-weight: 700; margin: 16px 0 8px; }
.preview-area p { margin-bottom: 12px; }
.preview-area blockquote {
  border-left: 3px solid var(--border);
  padding-left: 16px;
  color: var(--muted);
  margin-bottom: 12px;
}
.preview-area code {
  font-family: var(--font-mono);
  font-size: 14px;
  background: var(--sidebar-bg);
  padding: 2px 6px;
  border-radius: 3px;
}
.preview-area pre {
  background: var(--sidebar-bg);
  padding: 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin-bottom: 12px;
}
.preview-area pre code {
  background: none;
  padding: 0;
}
.preview-area img {
  max-width: 100%;
  border-radius: var(--radius);
  margin: 8px 0;
}
.preview-area a {
  color: var(--accent);
  text-decoration: none;
}
.preview-area a:hover {
  text-decoration: underline;
}
.preview-area a.wiki-link {
  color: var(--fg);
  border-bottom: 1px dashed var(--muted);
  text-decoration: none;
}
.preview-area a.wiki-link:hover {
  border-bottom-style: solid;
}
.preview-area ul, .preview-area ol {
  padding-left: 24px;
  margin-bottom: 12px;
}
.preview-area li {
  margin-bottom: 4px;
}
.preview-area hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 24px 0;
}

/* マークダウンツールバー */
.md-toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  border-top: 1px solid var(--border);
  background: var(--toolbar-bg);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.md-toolbar::-webkit-scrollbar {
  display: none;
}

.md-toolbar button {
  min-width: 36px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-family: var(--font-mono);
  border-radius: var(--radius);
  transition: background var(--transition);
  flex-shrink: 0;
}

.md-toolbar button:hover {
  background: var(--sidebar-hover);
}

.md-toolbar button:active {
  background: var(--sidebar-active);
}

/* ============================================
 * 保存インジケーター
 * ============================================ */
.save-indicator {
  position: fixed;
  top: 12px;
  right: 12px;
  font-size: 12px;
  color: var(--muted);
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  z-index: 100;
}

.save-indicator.show {
  opacity: 1;
}

.save-indicator.saving {
  color: var(--accent);
}

.save-indicator.error {
  color: var(--error);
}

/* ============================================
 * コンテキストメニュー
 * ============================================ */
.context-menu {
  position: fixed;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  padding: 4px 0;
  min-width: 160px;
  z-index: 200;
  display: none;
}

.context-menu.show {
  display: block;
}

.context-menu-item {
  display: block;
  width: 100%;
  padding: 8px 16px;
  font-size: 13px;
  text-align: left;
  transition: background var(--transition);
}

.context-menu-item:hover {
  background: var(--sidebar-hover);
}

.context-menu-item.danger {
  color: var(--error);
}

.context-menu-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* ============================================
 * モーダル
 * ============================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

.modal-overlay.show {
  display: flex;
}

.modal-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  width: 320px;
  max-width: 90vw;
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
}

.modal-input {
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--fg);
  margin-bottom: 16px;
}

.modal-input:focus {
  border-color: var(--accent);
  outline: none;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-btn {
  padding: 8px 16px;
  font-size: 13px;
  border-radius: var(--radius);
  transition: background var(--transition);
}

.modal-btn-primary {
  background: var(--accent);
  color: #fff;
}

.modal-btn-primary:hover {
  background: var(--accent-hover);
}

.modal-btn-cancel {
  background: var(--sidebar-bg);
}

.modal-btn-cancel:hover {
  background: var(--sidebar-hover);
}

/* ============================================
 * モバイル対応
 * ============================================ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    box-shadow: 4px 0 16px rgba(0,0,0,0.15);
  }

  .sidebar.hidden {
    transform: translateX(-100%);
  }

  /* モバイルではサイドバーの裏を暗くする */
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 9;
    display: none;
  }

  .sidebar-backdrop.show {
    display: block;
  }

  .editor-textarea {
    padding: 16px 20px;
    font-size: 15px;
  }

  .preview-area {
    padding: 16px 20px;
  }

  .editor-header {
    padding: 8px 12px;
  }

  .meta-date {
    display: none;
  }
}

/* セーフエリア（iPhone ノッチ対応） */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .md-toolbar {
    padding-bottom: calc(6px + env(safe-area-inset-bottom));
  }
}

/* ============================================
 * スクロールバー
 * ============================================ */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
}

/* ============================================
 * 画像プレースホルダー（アップロード中）
 * ============================================ */
.upload-placeholder {
  display: inline-block;
  color: var(--muted);
  font-style: italic;
  font-size: 13px;
}
