:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-app: #fbfcfe;
  --bg-panel: #ffffff;
  --bg-subtle: #f3f6fb;
  --bg-hover: #eef3fa;
  --bg-selected: #e8f0fe;
  --border: #e3e9f2;
  --border-strong: #cdd8e6;
  --text-primary: #1a2130;
  --text-secondary: #5c6a7e;
  --text-tertiary: #97a3b5;
  --accent: #2f6fed;
  --accent-hover: #1f5bd6;
  --accent-soft: #e9f1fe;
  --accent-soft-strong: #d7e6fd;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --warning: #ea580c;
  --warning-soft: #fdf0e4;
  --success: #16803d;
  --success-soft: #e4f6ea;

  --ball-self-bg: #e3edfd;   --ball-self-fg: #1f5bd6;
  --ball-client-bg: #fdecd9; --ball-client-fg: #b6540a;
  --ball-boss-bg: #efe6fb;   --ball-boss-fg: #7c3aed;
  --ball-office-bg: #dcf5e3; --ball-office-fg: #1f8a4c;
  --ball-expert-bg: #d7f2e8; --ball-expert-fg: #0f8f63;
  --ball-other-bg: #eef0f3;  --ball-other-fg: #5b6472;

  --shadow-sm: 0 1px 2px rgba(20, 30, 50, 0.06);
  --shadow-md: 0 6px 20px rgba(20, 30, 50, 0.10);
  --shadow-modal: 0 20px 60px rgba(20, 30, 50, 0.22);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Yu Gothic", "Noto Sans JP", "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161f;
    --bg-app: #0e1218;
    --bg-panel: #161b25;
    --bg-subtle: #1b212c;
    --bg-hover: #202836;
    --bg-selected: #22314e;
    --border: #2a3140;
    --border-strong: #38414f;
    --text-primary: #eef1f6;
    --text-secondary: #a3adbd;
    --text-tertiary: #6c7688;
    --accent: #5b8def;
    --accent-hover: #7aa2f3;
    --accent-soft: #1c2740;
    --accent-soft-strong: #243352;
    --danger: #f36a6a;
    --danger-soft: #3a1f22;
    --warning: #f0955a;
    --warning-soft: #3a2716;
    --success: #4fbd7b;
    --success-soft: #17301f;

    --ball-self-bg: #1c2c4d;   --ball-self-fg: #8fb4f7;
    --ball-client-bg: #3c2c17; --ball-client-fg: #f0ab6a;
    --ball-boss-bg: #2c2044;  --ball-boss-fg: #c4a4f7;
    --ball-office-bg: #163826; --ball-office-fg: #6fd699;
    --ball-expert-bg: #123329; --ball-expert-fg: #5fd0ac;
    --ball-other-bg: #262c36;  --ball-other-fg: #a3adbd;
  }
}
:root[data-theme="dark"] {
  --bg: #12161f; --bg-app: #0e1218; --bg-panel: #161b25; --bg-subtle: #1b212c;
  --bg-hover: #202836; --bg-selected: #22314e; --border: #2a3140; --border-strong: #38414f;
  --text-primary: #eef1f6; --text-secondary: #a3adbd; --text-tertiary: #6c7688;
  --accent: #5b8def; --accent-hover: #7aa2f3; --accent-soft: #1c2740; --accent-soft-strong: #243352;
  --danger: #f36a6a; --danger-soft: #3a1f22; --warning: #f0955a; --warning-soft: #3a2716;
  --success: #4fbd7b; --success-soft: #17301f;
  --ball-self-bg: #1c2c4d; --ball-self-fg: #8fb4f7;
  --ball-client-bg: #3c2c17; --ball-client-fg: #f0ab6a;
  --ball-boss-bg: #2c2044; --ball-boss-fg: #c4a4f7;
  --ball-office-bg: #163826; --ball-office-fg: #6fd699;
  --ball-expert-bg: #123329; --ball-expert-fg: #5fd0ac;
  --ball-other-bg: #262c36; --ball-other-fg: #a3adbd;
}
:root[data-theme="light"] {
  --bg: #ffffff; --bg-app: #fbfcfe; --bg-panel: #ffffff; --bg-subtle: #f3f6fb;
  --bg-hover: #eef3fa; --bg-selected: #e8f0fe; --border: #e3e9f2; --border-strong: #cdd8e6;
  --text-primary: #1a2130; --text-secondary: #5c6a7e; --text-tertiary: #97a3b5;
  --accent: #2f6fed; --accent-hover: #1f5bd6; --accent-soft: #e9f1fe; --accent-soft-strong: #d7e6fd;
  --danger: #dc2626; --danger-soft: #fdecec; --warning: #ea580c; --warning-soft: #fdf0e4;
  --success: #16803d; --success-soft: #e4f6ea;
  --ball-self-bg: #e3edfd; --ball-self-fg: #1f5bd6;
  --ball-client-bg: #fdecd9; --ball-client-fg: #b6540a;
  --ball-boss-bg: #efe6fb; --ball-boss-fg: #7c3aed;
  --ball-office-bg: #dcf5e3; --ball-office-fg: #1f8a4c;
  --ball-expert-bg: #d7f2e8; --ball-expert-fg: #0f8f63;
  --ball-other-bg: #eef0f3; --ball-other-fg: #5b6472;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg-app);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "palt";
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::selection { background: var(--accent-soft-strong); }
.tnum { font-variant-numeric: tabular-nums; }

#app { height: 100vh; display: flex; flex-direction: column; }

/* ---------- Header ---------- */
.topbar {
  flex-shrink: 0;
  padding: 18px 28px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  display: flex; flex-direction: column; gap: 14px;
}
.topbar-row1 { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.app-title { font-size: 20px; font-weight: 700; letter-spacing: 0.01em; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border-strong); background: var(--bg-panel);
  color: var(--text-primary); border-radius: var(--radius-md);
  padding: 8px 14px; font-size: 13.5px; font-weight: 500;
  transition: background .12s ease, border-color .12s ease;
}
.btn:hover { background: var(--bg-hover); }
.btn-primary {
  background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--bg-hover); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; border-radius: var(--radius-sm); }
.btn svg { width: 15px; height: 15px; flex-shrink: 0; }

.topbar-row2 { display: flex; align-items: center; gap: 10px; }
.search-box {
  flex: 1; display: flex; align-items: center; gap: 8px;
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 8px 12px;
}
.search-box:focus-within { border-color: var(--accent); background: var(--bg-panel); }
.search-box svg { width: 16px; height: 16px; color: var(--text-tertiary); flex-shrink: 0; }
.search-box input { flex: 1; border: none; background: transparent; outline: none; font-size: 13.5px; }
.sort-select, .select-control {
  border: 1px solid var(--border-strong); background: var(--bg-panel);
  border-radius: var(--radius-md); padding: 8px 10px; font-size: 13px; color: var(--text-primary);
}
.list-meta {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 12px; color: var(--text-tertiary); padding: 0 2px;
}

/* ---------- Main layout ---------- */
.main { flex: 1; min-height: 0; display: flex; }
.list-pane {
  width: clamp(320px, 34vw, 440px); flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0; background: var(--bg);
}
.list-pane-header { padding: 14px 20px 10px; display: flex; flex-direction: column; gap: 10px; }
.list-scroll { flex: 1; overflow-y: auto; padding: 4px 10px 20px; }
.detail-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg-app); }

.group-label {
  font-size: 11px; font-weight: 700; color: var(--text-tertiary);
  letter-spacing: 0.06em; padding: 12px 10px 6px;
}

/* ---------- List row ---------- */
.row {
  position: relative;
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 10px; border-radius: var(--radius-md);
  cursor: pointer; border: 1px solid transparent;
}
.row:hover { background: var(--bg-hover); }
.row.selected { background: var(--bg-selected); border-color: var(--accent-soft-strong); }
.row.drag-over-before::before, .row.drag-over-after::after {
  content: ""; position: absolute; left: 8px; right: 8px; height: 2px; background: var(--accent); border-radius: 2px;
}
.row.drag-over-before::before { top: -2px; }
.row.drag-over-after::after { bottom: -2px; }
.row.dragging { opacity: 0.4; }
.row-handle {
  flex-shrink: 0; width: 16px; height: 32px; display: flex; align-items: center; justify-content: center;
  color: var(--text-tertiary); cursor: grab; border-radius: 4px; margin-top: 2px;
}
.row-handle:hover { background: var(--bg-subtle); color: var(--text-secondary); }
.row-handle svg { width: 12px; height: 12px; }
.row-pin {
  flex-shrink: 0; width: 26px; height: 26px; display: flex; align-items: center; justify-content: center;
  border-radius: 6px; color: var(--text-tertiary); margin-top: 1px;
}
.row-pin:hover { background: var(--bg-subtle); }
.row-pin.active { color: var(--accent); }
.row-pin svg { width: 15px; height: 15px; }
.row-body { flex: 1; min-width: 0; }
.row-top { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
.row-client { font-size: 14.5px; font-weight: 700; color: var(--text-primary); }
.row-type-badge {
  font-size: 10.5px; font-weight: 600; color: var(--text-secondary); background: var(--bg-subtle);
  border: 1px solid var(--border); padding: 1px 7px; border-radius: 20px; white-space: nowrap;
}
.row-case-name { font-size: 12.5px; color: var(--text-secondary); margin-top: 1px; }
.row-status {
  font-size: 12.5px; color: var(--text-tertiary); margin-top: 5px; line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.row-meta { flex-shrink: 0; text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; margin-top: 1px; }
.badge {
  font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 20px; white-space: nowrap;
}
.next-date { font-size: 12px; font-weight: 600; white-space: nowrap; }
.next-date.overdue, .next-date.today { color: var(--danger); }
.next-date.tomorrow { color: var(--warning); }
.next-date.normal { color: var(--text-secondary); font-weight: 500; }
.next-date.unset { color: var(--text-tertiary); font-weight: 500; }
.row-updated { font-size: 10.5px; color: var(--text-tertiary); }

.empty-state {
  text-align: center; color: var(--text-tertiary); font-size: 13px; padding: 60px 20px; line-height: 1.8;
}

/* ---------- Detail pane ---------- */
.detail-empty {
  flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--text-tertiary); font-size: 13.5px; flex-direction: column; gap: 10px;
}
.detail-empty svg { width: 34px; height: 34px; opacity: 0.4; }

.detail-sticky {
  flex-shrink: 0; background: var(--bg-panel); border-bottom: 1px solid var(--border);
  padding: 18px 28px 16px; display: flex; flex-direction: column; gap: 12px;
}
.detail-sticky-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.detail-back { display: none; }
.detail-titles { min-width: 0; }
.detail-client-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.detail-client { font-size: 19px; font-weight: 700; }
.detail-case-name { font-size: 14px; color: var(--text-secondary); margin-top: 3px; }
.detail-sticky-facts { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: 12.5px; color: var(--text-secondary); }
.detail-sticky-facts .fact { display: flex; align-items: center; gap: 6px; }
.detail-sticky-facts .fact-label { color: var(--text-tertiary); }

.detail-scroll { flex: 1; overflow-y: auto; padding: 22px 28px 60px; }
.section { max-width: 720px; margin: 0 auto 30px; }
.section:last-child { margin-bottom: 0; }
.section-title {
  display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 700;
  color: var(--text-secondary); margin-bottom: 10px; letter-spacing: 0.02em;
}
.section-bar { width: 3px; height: 13px; border-radius: 2px; background: var(--accent); }
.card {
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 16px 18px;
}

.status-text {
  white-space: pre-wrap; line-height: 1.7; font-size: 13.5px; min-height: 24px; cursor: text; border-radius: var(--radius-sm);
}
.status-text.placeholder { color: var(--text-tertiary); }
.status-edit-textarea {
  width: 100%; border: 1px solid var(--accent); border-radius: var(--radius-sm); padding: 8px 10px;
  resize: vertical; min-height: 90px; line-height: 1.7; font-size: 13.5px; outline: none; background: var(--bg-panel);
  color: var(--text-primary);
}
.edit-hint { font-size: 11px; color: var(--text-tertiary); margin-top: 8px; }
.status-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

.field-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.field-row + .field-row { margin-top: 10px; }
.field-label { font-size: 12px; color: var(--text-tertiary); width: 72px; flex-shrink: 0; }

select.control, input.control {
  border: 1px solid var(--border-strong); background: var(--bg); border-radius: var(--radius-sm);
  padding: 7px 10px; font-size: 13px; color: var(--text-primary);
}
.clear-btn { font-size: 12px; color: var(--text-tertiary); background: none; border: none; text-decoration: underline; padding: 2px 4px; }
.clear-btn:hover { color: var(--text-secondary); }

.action-item { display: flex; align-items: flex-start; gap: 9px; padding: 6px 0; }
.action-item + .action-item { border-top: 1px dashed var(--border); }
.action-item input[type="checkbox"] { margin-top: 3px; width: 15px; height: 15px; accent-color: var(--accent); flex-shrink: 0; }
.action-text { font-size: 13.5px; line-height: 1.6; }
.action-item.done .action-text { text-decoration: line-through; color: var(--text-tertiary); }
.add-action-row { margin-top: 10px; }
.add-action-input-row { display: flex; gap: 8px; margin-top: 10px; }
.add-action-input-row input { flex: 1; }

.memo-text {
  white-space: pre-wrap; line-height: 1.7; font-size: 13.5px; min-height: 90px; cursor: text; border-radius: var(--radius-sm);
}
.memo-text.placeholder { color: var(--text-tertiary); }
.memo-edit-textarea {
  width: 100%; border: 1px solid var(--accent); border-radius: var(--radius-sm); padding: 8px 10px;
  resize: vertical; min-height: 96px; line-height: 1.7; font-size: 13.5px; outline: none; background: var(--bg-panel);
  color: var(--text-primary);
}

.drop-zone {
  margin-top: 10px; border: 1.5px dashed var(--border-strong); border-radius: var(--radius-md);
  padding: 18px; text-align: center; font-size: 12.5px; color: var(--text-tertiary);
}
.drop-zone.drag-active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.attachment-list { display: flex; flex-direction: column; gap: 8px; }
.attachment-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--bg-subtle); border-radius: var(--radius-sm); }
.attachment-icon {
  width: 30px; height: 30px; border-radius: 7px; display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700; color: #fff; flex-shrink: 0;
}
.attachment-name { font-size: 12.5px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-size { font-size: 11px; color: var(--text-tertiary); flex-shrink: 0; }
.attach-caption { font-size: 11px; color: var(--text-tertiary); margin-top: 10px; }

.history-day { margin-bottom: 16px; }
.history-day:last-child { margin-bottom: 0; }
.history-date { font-size: 12px; font-weight: 700; color: var(--text-secondary); margin-bottom: 8px; }
.history-entry { display: flex; gap: 8px; padding: 6px 0; font-size: 13px; line-height: 1.6; }
.history-entry + .history-entry { border-top: 1px dashed var(--border); }
.history-time { color: var(--text-tertiary); font-size: 11.5px; flex-shrink: 0; width: 40px; padding-top: 1px; }
.history-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.history-line { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
.history-tag {
  flex-shrink: 0; font-size: 10.5px; font-weight: 600; padding: 1px 7px; border-radius: 20px; height: fit-content;
}
.history-tag.auto { background: var(--bg-subtle); color: var(--text-tertiary); }
.history-tag.manual { background: var(--accent-soft); color: var(--accent); }
.history-text { color: var(--text-primary); }
.history-quote {
  margin-top: 2px; padding: 8px 10px; background: var(--bg-subtle); border-left: 2px solid var(--border-strong);
  border-radius: 4px; font-size: 12.5px; color: var(--text-secondary); white-space: pre-wrap; line-height: 1.6;
}
.history-quote-toggle {
  align-self: flex-start; font-size: 11.5px; color: var(--accent); background: none; border: none; padding: 0;
  text-decoration: underline; cursor: pointer;
}
.add-history-row { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.add-history-row select { flex-shrink: 0; }
.add-history-row input { flex: 1; min-width: 140px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 20, 30, 0.4); display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 20px;
}
.modal {
  background: var(--bg-panel); border-radius: var(--radius-lg); box-shadow: var(--shadow-modal);
  width: 100%; max-width: 440px; padding: 24px; display: flex; flex-direction: column; gap: 16px;
}
.modal h2 { margin: 0; font-size: 16px; font-weight: 700; }
.modal-field { display: flex; flex-direction: column; gap: 6px; position: relative; }
.modal-field label { font-size: 12px; font-weight: 600; color: var(--text-secondary); }
.modal-field input, .modal-field select {
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); padding: 9px 11px; font-size: 13.5px;
  background: var(--bg); color: var(--text-primary);
}
.modal-field input:focus, .modal-field select:focus { border-color: var(--accent); }
.modal-hint { font-size: 11px; color: var(--text-tertiary); }
.autocomplete-list {
  position: absolute; top: 100%; left: 0; right: 0; margin-top: 4px; background: var(--bg-panel);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); box-shadow: var(--shadow-md);
  max-height: 160px; overflow-y: auto; z-index: 5;
}
.autocomplete-item { padding: 8px 11px; font-size: 13px; cursor: pointer; }
.autocomplete-item:hover, .autocomplete-item.active { background: var(--bg-hover); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- Mobile FAB ---------- */
.fab {
  display: none; position: fixed; right: 20px; bottom: 24px; width: 54px; height: 54px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none; align-items: center; justify-content: center;
  box-shadow: var(--shadow-md); z-index: 20;
}
.fab svg { width: 24px; height: 24px; }

/* ---------- Responsive ---------- */
@media (max-width: 899px) {
  .topbar { padding: 14px 16px 12px; }
  .btn.new-case-btn { display: none; }
  .fab { display: flex; }
  .main { position: relative; overflow: hidden; }
  .list-pane { width: 100%; border-right: none; }
  .detail-pane { position: absolute; inset: 0; background: var(--bg-app); transform: translateX(100%); transition: transform .18s ease; }
  .main.mobile-detail-active .list-pane { display: none; }
  .main.mobile-detail-active .detail-pane { transform: translateX(0); }
  .detail-back { display: flex; }
  .detail-sticky { padding: 14px 16px 14px; }
  .detail-scroll { padding: 18px 16px 90px; }
  .row-status { -webkit-line-clamp: 3; }
  .field-label { width: 64px; }
}
@media (prefers-reduced-motion: reduce) {
  .detail-pane { transition: none !important; }
}

/* ---------- Auth gate ---------- */
.auth-loading {
  height: 100vh; display: flex; align-items: center; justify-content: center;
  color: var(--text-tertiary); font-size: 13px; background: var(--bg-app); padding: 20px; text-align: center;
}
.auth-gate {
  height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--bg-app); padding: 20px;
}
.auth-card {
  width: 100%; max-width: 360px; background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-md); padding: 32px 28px;
  display: flex; flex-direction: column; gap: 8px;
}
.auth-title { margin: 0; font-size: 22px; font-weight: 700; color: var(--text-primary); }
.auth-sub { margin: 0 0 12px; font-size: 13px; color: var(--text-secondary); line-height: 1.6; }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form .control { padding: 10px 12px; font-size: 14px; }
.auth-message {
  margin: 12px 0 0; padding: 10px 12px; background: var(--accent-soft); color: var(--accent);
  border-radius: var(--radius-sm); font-size: 12.5px; line-height: 1.6;
}
.auth-user-email { font-size: 12px; color: var(--text-tertiary); align-self: center; }

#app[hidden], .auth-gate[hidden], .auth-loading[hidden] { display: none !important; }
