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

:root {
  --sidebar-w: 220px;
  --mobile-nav-h: 60px;
  --sidebar-bg: #0f172a;
  --sidebar-text: #94a3b8;
  --sidebar-active-bg: rgba(99,102,241,.15);
  --sidebar-active-text: #e0e7ff;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --danger: #ef4444;
  --warning: #f59e0b;
  --success: #22c55e;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
}

html { -webkit-text-size-adjust: 100%; overflow-x: hidden; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; overflow-x: hidden; max-width: 100vw; }

/* ===== ユーティリティ ===== */
.hidden { display: none !important; }
.error-msg { color: var(--danger); font-size: .875rem; margin-top: .5rem; }

/* ===== ログイン画面 ===== */
#login-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: var(--sidebar-bg); padding: 1rem;
}
.login-card {
  background: var(--card); border-radius: var(--radius);
  padding: 2.5rem 2rem; width: 100%; max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.login-logo {
  font-size: 1.5rem; font-weight: 700; color: var(--accent);
  text-align: center; margin-bottom: 1.75rem; letter-spacing: -.5px;
}

/* ===== メイン画面 ===== */
#main-screen { display: flex; min-height: 100vh; }

/* ===== サイドバー（PC） ===== */
#sidebar {
  width: var(--sidebar-w); background: var(--sidebar-bg);
  display: flex; flex-direction: column;
  position: fixed; top: 0; left: 0; height: 100vh;
  z-index: 100;
}
.sidebar-header {
  padding: 1.25rem 1rem; font-weight: 700; font-size: 1.05rem;
  color: #f1f5f9; letter-spacing: -.3px; border-bottom: 1px solid rgba(255,255,255,.06);
}
.sidebar-nav { flex: 1; padding: .5rem 0; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: .65rem;
  padding: .65rem 1rem; color: var(--sidebar-text);
  text-decoration: none; font-size: .9rem; font-weight: 500;
  border-radius: 6px; margin: 2px 8px; transition: all .15s;
}
.nav-item svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.nav-item:hover { background: rgba(255,255,255,.06); color: #e2e8f0; }
.nav-item.active { background: var(--sidebar-active-bg); color: var(--sidebar-active-text); }
.sidebar-footer { padding: .75rem; border-top: 1px solid rgba(255,255,255,.06); }

/* ===== コンテンツエリア ===== */
#content-wrapper { margin-left: var(--sidebar-w); flex: 1; min-height: 100vh; }
#page-content { padding: 1.75rem; max-width: 1200px; }

/* ===== モバイルナビ（デフォルト非表示） ===== */
#mobile-nav { display: none; }

/* ===== フォーム ===== */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: .85rem; font-weight: 600; color: var(--text-muted); margin-bottom: .35rem; }
input[type=text], input[type=password], input[type=number], input[type=date],
input[type=datetime-local], textarea, select {
  width: 100%; padding: .6rem .75rem; border: 1.5px solid var(--border);
  border-radius: 8px; font-size: 1rem; color: var(--text); background: var(--card);
  transition: border-color .15s; outline: none; font-family: inherit;
  -webkit-appearance: none; appearance: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99,102,241,.1); }
textarea { resize: vertical; line-height: 1.6; }
select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2394a3b8'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right .6rem center; background-size: 16px; padding-right: 2rem; }

/* ===== ボタン ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem 1.1rem; border-radius: 8px; font-size: .9rem; font-weight: 600;
  cursor: pointer; border: none; transition: all .15s; text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover, .btn-primary:active { background: var(--accent-hover); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover, .btn-secondary:active { background: #cbd5e1; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover, .btn-danger:active { background: #dc2626; }
.btn-ghost { background: transparent; color: var(--sidebar-text); width: 100%; }
.btn-ghost:hover { background: rgba(255,255,255,.06); color: #e2e8f0; }
.btn-full { width: 100%; }
.btn-sm { padding: .35rem .75rem; font-size: .8rem; }
.btn-icon { padding: .5rem; border-radius: 6px; background: transparent; border: none; cursor: pointer; color: var(--text-muted); display: inline-flex; transition: all .15s; -webkit-tap-highlight-color: transparent; }
.btn-icon:hover { background: var(--border); color: var(--text); }

/* ===== ページヘッダー ===== */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; }
.page-title { font-size: 1.4rem; font-weight: 700; letter-spacing: -.3px; }

/* ===== カード ===== */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); }
.card-pad { padding: 1rem; }

/* ===== 検索バー ===== */
.search-bar { position: relative; margin-bottom: 1rem; }
.search-bar input { padding-left: 2.4rem; }
.search-bar::before {
  content: ""; position: absolute; left: .75rem; top: 50%; transform: translateY(-50%);
  width: 16px; height: 16px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2394a3b8' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z'/%3E%3C/svg%3E") center/cover no-repeat;
  pointer-events: none;
}

/* ===== タグ ===== */
.tag {
  display: inline-block; padding: .15rem .5rem; background: #ede9fe;
  color: #6d28d9; border-radius: 99px; font-size: .75rem; font-weight: 600;
}

/* ===== メモ一覧グリッド ===== */
.notes-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.note-card { cursor: pointer; transition: box-shadow .15s, transform .15s; -webkit-tap-highlight-color: transparent; }
.note-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,.1); transform: translateY(-2px); }
.note-card-body { padding: 1rem; }
.note-card-title { font-weight: 700; font-size: 1rem; margin-bottom: .4rem; }
.note-card-preview { font-size: .83rem; color: var(--text-muted); line-height: 1.5; margin-bottom: .6rem; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.note-card-footer { display: flex; align-items: center; justify-content: space-between; font-size: .75rem; color: var(--text-muted); margin-top: .5rem; }
.note-card-tags { display: flex; gap: .3rem; flex-wrap: wrap; }

/* ===== エディタ ===== */
.editor-pane { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; height: 60vh; }
.editor-pane textarea { height: 100%; resize: none; font-family: 'SF Mono', 'Consolas', monospace; font-size: .875rem; }
.preview-pane { height: 100%; overflow-y: auto; padding: .75rem; border: 1.5px solid var(--border); border-radius: 6px; background: #fafafa; }
.preview-label { font-size: .75rem; font-weight: 600; color: var(--text-muted); margin-bottom: .3rem; }

/* markdownスタイル */
.preview-pane h1,.preview-pane h2,.preview-pane h3 { margin: 1rem 0 .5rem; }
.preview-pane p { margin-bottom: .75rem; line-height: 1.7; }
.preview-pane ul,.preview-pane ol { padding-left: 1.5rem; margin-bottom: .75rem; }
.preview-pane code { background: #f1f5f9; padding: .1rem .3rem; border-radius: 4px; font-size: .85em; }
.preview-pane pre { background: #f1f5f9; padding: .75rem; border-radius: 6px; overflow-x: auto; margin-bottom: .75rem; }
.preview-pane blockquote { border-left: 3px solid var(--accent); padding-left: .75rem; color: var(--text-muted); margin-bottom: .75rem; }

/* ===== モーダル ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 1rem;
}
.modal { background: var(--card); border-radius: var(--radius); width: 100%; max-width: 900px; max-height: 90vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-sm { max-width: 480px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.modal-header h2 { font-size: 1.05rem; font-weight: 700; }
.modal-body { flex: 1; overflow-y: auto; padding: 1.25rem; -webkit-overflow-scrolling: touch; }
.modal-footer { display: flex; gap: .5rem; align-items: center; padding: 1rem 1.25rem; border-top: 1px solid var(--border); flex-shrink: 0; }

/* ===== カンバン ===== */
.kanban { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; align-items: start; }
.kanban-col { background: var(--bg); border-radius: var(--radius); border: 1px solid var(--border); }
.kanban-col-header { padding: .75rem 1rem; font-weight: 700; font-size: .9rem; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.kanban-col-header .col-count { font-size: .75rem; font-weight: 600; background: var(--border); padding: .1rem .45rem; border-radius: 99px; color: var(--text-muted); }
.kanban-cards { padding: .75rem; display: flex; flex-direction: column; gap: .5rem; min-height: 80px; }
.todo-card { background: var(--card); border-radius: 8px; padding: .75rem; box-shadow: var(--shadow); border: 1px solid var(--border); border-left: 3px solid var(--border); cursor: pointer; transition: box-shadow .15s; -webkit-tap-highlight-color: transparent; }
.todo-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,.1); }
.todo-card.priority-2 { border-left-color: var(--danger); }
.todo-card.priority-1 { border-left-color: var(--warning); }
.todo-card.priority-0 { border-left-color: var(--success); }
.todo-card-title { font-weight: 600; font-size: .9rem; margin-bottom: .4rem; }
.todo-card-meta { font-size: .75rem; color: var(--text-muted); display: flex; gap: .75rem; flex-wrap: wrap; }
.todo-card-actions { display: flex; gap: .25rem; margin-top: .5rem; }
.due-overdue { color: var(--danger) !important; font-weight: 600; }

/* ===== バッジ・優先度 ===== */
.badge { display: inline-block; padding: .15rem .5rem; border-radius: 99px; font-size: .75rem; font-weight: 600; }
.badge-todo { background: #f1f5f9; color: #475569; }
.badge-doing { background: #fef3c7; color: #92400e; }
.badge-done { background: #dcfce7; color: #166534; }
.priority-badge-2 { color: var(--danger); font-weight: 700; }
.priority-badge-1 { color: var(--warning); font-weight: 700; }
.priority-badge-0 { color: var(--success); font-weight: 700; }

/* ===== トースト通知 ===== */
#toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%);
  z-index: 999; background: #1e293b; color: #f1f5f9; padding: .7rem 1.25rem;
  border-radius: 99px; font-size: .875rem; font-weight: 500; white-space: nowrap;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); opacity: 0;
  transition: opacity .2s; pointer-events: none;
}
#toast.show { opacity: 1; }

/* ===== 健康ページ入力行 ===== */
.health-record-form { display: grid; grid-template-columns: 1fr 1fr 2fr; gap: .75rem; align-items: end; }

/* ===== AI チャット ===== */
.ai-chat-card {
  display: flex; flex-direction: column;
  height: calc(100vh - 340px); min-height: 380px;
}
.ai-messages {
  flex: 1; overflow-y: auto; padding: 1rem;
  display: flex; flex-direction: column; gap: .75rem;
  -webkit-overflow-scrolling: touch;
}
.ai-msg { display: flex; }
.ai-msg-user { justify-content: flex-end; }
.ai-msg-assistant { justify-content: flex-start; }
.ai-bubble {
  max-width: 80%; padding: .65rem 1rem;
  border-radius: 18px; font-size: .9rem; line-height: 1.65; word-break: break-word;
}
.ai-msg-user .ai-bubble {
  background: var(--accent); color: #fff;
  border-radius: 18px 18px 4px 18px;
}
.ai-msg-assistant .ai-bubble {
  background: #f1f5f9; color: var(--text);
  border-radius: 18px 18px 18px 4px;
}
.ai-input-area {
  display: flex; gap: .5rem; padding: .75rem;
  border-top: 1px solid var(--border); align-items: flex-end;
}
.ai-input-area textarea {
  flex: 1; resize: none; border-radius: 12px;
  padding: .6rem .85rem; font-size: .9rem; line-height: 1.5;
}
.ai-bubble-loading { color: var(--text-muted); }
.ai-dots span { animation: ai-blink 1.2s infinite; display: inline-block; }
.ai-dots span:nth-child(2) { animation-delay: .2s; }
.ai-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes ai-blink { 0%,80%,100% { opacity:0; } 40% { opacity:1; } }

/* ===== モバイル ===== */
@media (max-width: 768px) {
  /* サイドバーを隠す */
  #sidebar { display: none; }
  #content-wrapper { margin-left: 0; padding-bottom: var(--mobile-nav-h); overflow-x: hidden; width: 100%; }
  #page-content { padding: 1rem; max-width: 100%; overflow-x: hidden; }

  /* ボトムナビ */
  #mobile-nav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0;
    height: var(--mobile-nav-h); background: var(--sidebar-bg);
    border-top: 1px solid rgba(255,255,255,.08);
    z-index: 100; align-items: stretch;
  }
  .mobile-nav-item {
    flex: 1; display: flex; flex-direction: column; align-items: center;
    justify-content: center; gap: 3px; color: var(--sidebar-text);
    text-decoration: none; font-size: .65rem; font-weight: 600;
    background: none; border: none; cursor: pointer; padding: .3rem 0;
    -webkit-tap-highlight-color: transparent; transition: color .15s;
  }
  .mobile-nav-item svg {
    width: 22px; height: 22px; stroke: currentColor; fill: none;
    stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
  }
  .mobile-nav-item.active { color: var(--accent); }
  .mobile-nav-item span { line-height: 1; }

  /* トーストをボトムナビの上に */
  #toast { bottom: calc(var(--mobile-nav-h) + .75rem); }

  /* モーダル：下から全画面表示 */
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal, .modal.modal-sm {
    max-width: 100%; border-radius: var(--radius) var(--radius) 0 0;
    max-height: 92vh;
  }

  /* フォーム */
  .page-title { font-size: 1.15rem; }
  .btn { padding: .6rem 1rem; min-height: 44px; }
  .btn-sm { padding: .4rem .75rem; font-size: .8rem; min-height: 36px; }
  .modal-footer { flex-wrap: wrap; }
  .modal-footer .btn { flex: 1; min-width: 80px; }

  /* エディタ：プレビュー非表示（編集に集中） */
  .editor-pane { grid-template-columns: 1fr; height: auto; }
  .editor-pane textarea { height: 50vh; }
  #nm-preview { display: none; }
  .preview-label:first-of-type { display: none; }

  /* メモグリッド */
  .notes-grid { grid-template-columns: 1fr; }

  /* カンバン：縦積み */
  .kanban { grid-template-columns: 1fr; gap: .75rem; }
  .kanban-col { min-width: unset; }

  /* 健康入力フォーム */
  .health-record-form { grid-template-columns: 1fr 1fr; }
  .health-record-form .health-note-row { grid-column: 1 / -1; }
  .metric-list-layout { grid-template-columns: 1fr !important; }

  /* テーブルをカード形式に */
  table { display: block; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* iOS safe area対応 */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #mobile-nav { padding-bottom: env(safe-area-inset-bottom); height: calc(var(--mobile-nav-h) + env(safe-area-inset-bottom)); }
  #content-wrapper { padding-bottom: calc(var(--mobile-nav-h) + env(safe-area-inset-bottom)); }
}
