:root {
  --font-display: "Yu Mincho", "YuMincho", "Hiragino Mincho ProN", "MS PMincho", serif;
  --font-body: "Yu Gothic Medium", "Yu Gothic", "Hiragino Sans", Meiryo, sans-serif;

  --ink: #1e2124;
  --ink-soft: #565c62;
  --paper: #fdfcfa;
  --canvas: #eef0ec;
  --panel: #ffffff;
  --line: #dcded8;
  --line-strong: #b9bcb4;

  --rust: #c1440e;
  --rust-deep: #8f3209;
  --rust-tint: #f4ded0;
  --rust-mid: #ecc0a4;
  --rust-light: #faf1ea;

  --charcoal: #23262b;
  --charcoal-mid: #d4d5d2;
  --charcoal-light: #f1f1f0;

  --stone: #6b6558;
  --stone-mid: #e3dcc9;
  --stone-light: #f7f4ec;

  --amber: #fbe9cd;
  --calc: #eceeea;
  --danger: #a3311c;
  --good: #2f6b3e;
  --info: #1f5a8f;
  --special: #6b3f8f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--canvas);
  font-size: 14px;
}

button, input, select, textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink);
  border-radius: 5px;
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
}

button:hover { border-color: var(--ink-soft); }

button.primary {
  background: var(--rust);
  border-color: var(--rust);
  color: #fff;
}

button.primary:hover {
  background: var(--rust-deep);
  border-color: var(--rust-deep);
}

button.danger { color: var(--danger); }
button.danger-solid {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
button.danger-solid:hover { background: #8a2917; border-color: #8a2917; }
button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
  padding: 6px 8px;
}
button.small { padding: 5px 9px; font-size: 12.5px; }

/* ---- アプリシェル（サイドバー＋トップバー＋メイン） ---- */

.crm-shell {
  display: flex;
  min-height: 100vh;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:hover { background: rgba(255, 255, 255, .12); border-color: transparent; }

/* サイドバー */
.sidebar {
  width: 248px;
  flex: 0 0 248px;
  background: var(--charcoal);
  border-right: 1px solid #33373d;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 30;
  transition: width .18s ease, flex-basis .18s ease;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 16px 14px;
  border-bottom: 1px solid #33373d;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  flex: 0 0 auto;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: var(--rust);
  color: #fff;
  font-weight: 800;
  font-size: 13px;
  letter-spacing: .04em;
  border-radius: 8px;
}
.brand-text { display: grid; line-height: 1.25; min-width: 0; }
.brand-text strong { color: #fff; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text small { color: #9aa0a6; font-size: 11px; }

.collapse-btn { color: #cfd2cd; }

.sidebar-nav { padding: 12px 10px; flex: 1 1 auto; overflow-y: auto; }
.nav-group-label {
  color: #7f868d;
  font-size: 10.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  font-weight: 700;
  padding: 6px 10px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 10px 10px;
  margin-bottom: 2px;
  border: 0;
  border-left: 3px solid transparent;
  border-radius: 0 6px 6px 0;
  background: transparent;
  color: #d3d6da;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}
.nav-item:hover { background: rgba(255, 255, 255, .07); border-color: transparent; }
.nav-item.active {
  background: rgba(193, 68, 14, .16);
  color: #fff;
  border-left-color: var(--rust);
}
.nav-icon { flex: 0 0 auto; display: inline-flex; }
.nav-icon svg { width: 19px; height: 19px; }
.nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-foot { padding: 10px; border-top: 1px solid #33373d; }

/* 折りたたみ状態 */
.sidebar.collapsed { width: 72px; flex-basis: 72px; }
.sidebar.collapsed .brand-text,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .nav-group-label { display: none; }
.sidebar.collapsed .sidebar-head { justify-content: center; }
.sidebar.collapsed .brand { display: none; }
.sidebar.collapsed .collapse-btn svg { transform: rotate(180deg); }
.sidebar.collapsed .nav-item { justify-content: center; gap: 0; border-radius: 8px; border-left-width: 0; }
.sidebar.collapsed .nav-item.active { border-left-width: 0; box-shadow: inset 0 0 0 1px var(--rust); }

/* メイン列 */
.main-col { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }

/* トップバー */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  height: 64px;
  padding: 0 20px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-title { font-size: 16px; font-weight: 700; color: var(--ink); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }

.menu-toggle { display: none; color: var(--ink); }

.root-status { font-size: 12px; color: var(--ink-soft); }
.root-status strong { color: var(--ink); }

.save-status {
  font-size: 12px;
  color: var(--ink-soft);
  min-width: 84px;
  text-align: right;
  transition: opacity .2s;
}
.save-status.saving { color: #a9761f; }
.save-status.saved { color: var(--good); }
.save-status.dirty { color: var(--rust-deep); }

/* メインコンテンツ */
.content {
  flex: 1 1 auto;
  padding: 24px 28px 48px;
  background: var(--canvas);
  width: 100%;
  max-width: 1360px;
}

.sidebar-overlay { display: none; }

/* 案件詳細の下位タブ（従来のタブUIを踏襲） */
.tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; }

.tab {
  border: 1px solid transparent;
  border-radius: 5px 5px 0 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--ink-soft);
  padding: 9px 16px;
  font-size: 13.5px;
  cursor: pointer;
}

.tab.active {
  background: var(--charcoal);
  color: #fff;
  border-color: var(--charcoal);
  border-bottom-color: var(--rust);
}

.tabs.sub .tab { padding: 7px 12px; font-size: 12.5px; }

.panel { display: none; }
.panel.active { display: block; }

.view { display: none; }
.view.active { display: block; }

.block {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 16px;
  margin-bottom: 14px;
}

.block h2 { margin: 0 0 12px; font-size: 16px; letter-spacing: .02em; }
.block h3 { margin: 0 0 8px; font-size: 13.5px; color: var(--ink-soft); }

.block-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 12px;
}

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

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 12px;
}
.form-grid.compact { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.form-grid.single { grid-template-columns: 1fr; }

label { display: grid; gap: 4px; color: var(--ink-soft); font-size: 12px; letter-spacing: .02em; }
label.wide { grid-column: 1 / -1; }

input, select, textarea {
  width: 100%;
  border: 1px solid #c7cac3;
  border-radius: 4px;
  padding: 7px 8px;
  background: var(--amber);
  color: var(--ink);
}
textarea { resize: vertical; min-height: 60px; }

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--rust-mid);
  outline-offset: -1px;
}

input[readonly], textarea[readonly] { background: var(--calc); color: #4a4f53; }

.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 5px; }

table { border-collapse: collapse; width: 100%; }

.edit-table { min-width: 720px; background: #fff; }
.edit-table.narrow { min-width: 0; }

th {
  background: var(--charcoal);
  color: #fff;
  font-weight: 700;
  text-align: center;
  border: 1px solid #3a3d41;
  padding: 8px 7px;
  white-space: nowrap;
  font-size: 12.5px;
  letter-spacing: .02em;
}

td { border: 1px solid var(--line); padding: 6px; vertical-align: middle; }
td.amount, .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

.edit-table input, .edit-table select { border: 0; border-radius: 0; padding: 6px; min-width: 72px; }

.summary-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin: 12px 0;
}
.summary-strip span {
  background: var(--canvas);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 10px;
  color: var(--ink-soft);
  display: block;
}
.summary-strip strong {
  display: block;
  margin-top: 4px;
  color: var(--ink);
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  background: var(--charcoal-mid);
  color: var(--charcoal);
}
.badge.rust { background: var(--rust-mid); color: var(--rust-deep); }
.badge.stone { background: var(--stone-mid); color: var(--stone); }
.badge.muted { background: var(--charcoal-mid); color: var(--charcoal); }
.badge.good { background: #d9ecdd; color: var(--good); }
.badge.danger { background: #f3d9d3; color: var(--danger); }
.badge.info { background: #d5e3f0; color: var(--info); }
.badge.special { background: #e6dcf0; color: var(--special); }

.profit-low { color: var(--danger); font-weight: 700; }
.profit-good { color: var(--good); font-weight: 700; }

.amount-negative { color: var(--danger); font-weight: 700; }

.loading-state {
  padding: 24px;
  text-align: center;
  color: var(--ink-soft);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.error-state {
  padding: 20px;
  display: grid;
  gap: 6px;
  color: var(--danger);
  background: #fbf1ee;
  border: 1px solid #e6c3ba;
  border-radius: 6px;
}
.error-state span { color: var(--ink-soft); font-size: 12.5px; }
.error-state button { justify-self: start; margin-top: 4px; }

.card-link {
  display: block;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 14px 16px;
  margin-bottom: 8px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}
.card-link:hover { border-color: var(--rust-mid); }
.card-link .card-title { font-weight: 700; font-size: 15px; }
.card-link .card-meta { color: var(--ink-soft); font-size: 12.5px; margin-top: 4px; }

.empty-state {
  padding: 28px;
  text-align: center;
  color: var(--ink-soft);
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  background: var(--panel);
}

/* ダッシュボード */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}
.kpi-card {
  background: var(--canvas);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px 16px;
}
.kpi-label { font-size: 12px; color: var(--ink-soft); }
.kpi-value {
  margin-top: 6px;
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: .01em;
}
.kpi-sub { margin-top: 4px; font-size: 12px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.alert-list { display: grid; gap: 6px; }
.alert-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-left-width: 4px;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 600;
}
.alert-row:hover { background: var(--canvas); border-color: var(--line-strong); }
.alert-dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; }
.alert-row.alert-danger { border-left-color: var(--danger); }
.alert-row.alert-danger .alert-dot { background: var(--danger); }
.alert-row.alert-warn { border-left-color: #b7791f; }
.alert-row.alert-warn .alert-dot { background: #b7791f; }
.alert-row.alert-info { border-left-color: var(--info); }
.alert-row.alert-info .alert-dot { background: var(--info); }

.setup-guide {
  padding: 8px 4px;
}
.setup-guide h3 { margin: 0 0 10px; font-size: 16px; color: var(--ink); }
.setup-guide p { margin: 0 0 10px; color: var(--ink-soft); line-height: 1.7; }
.setup-guide ol { margin: 0 0 10px; padding-left: 1.3em; color: var(--ink); line-height: 1.9; }
.setup-guide .setup-note { font-size: 12.5px; color: var(--ink-soft); }

.breadcrumb {
  font-size: 12.5px;
  color: var(--ink-soft);
  margin-bottom: 10px;
}
.breadcrumb button {
  border: none;
  background: none;
  padding: 0;
  color: var(--rust-deep);
  font-weight: 700;
  cursor: pointer;
}
.breadcrumb button:hover { text-decoration: underline; }

.project-photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.project-photo-grid figure {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 5px;
  overflow: hidden;
  background: #fff;
}
.project-photo-grid img { width: 100%; height: 110px; object-fit: cover; display: block; }
.project-photo-grid figcaption { padding: 5px 7px; font-size: 11px; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.estimate-frame-wrap { border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: #fff; }
.estimate-frame-wrap iframe { width: 100%; height: 78vh; border: 0; display: block; }

.hidden { display: none; }

dialog {
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 20px 22px;
  max-width: min(560px, calc(100vw - 32px));
  box-shadow: 0 12px 40px rgba(30, 33, 36, .28);
}
dialog::backdrop { background: rgba(30, 33, 36, .4); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
.confirm-message { margin: 0; color: var(--ink-soft); line-height: 1.6; white-space: pre-line; }

@media (max-width: 1023px) {
  .content { padding: 20px; }
  .form-grid.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* タブレット以下：サイドバーは折りたたみ基調のまま表示 */
@media (max-width: 900px) {
  .sidebar { width: 72px; flex-basis: 72px; }
  .sidebar .brand-text, .sidebar .nav-label, .sidebar .nav-group-label { display: none; }
  .sidebar .sidebar-head { justify-content: center; }
  .sidebar .collapse-btn { display: none; }
  .sidebar .nav-item { justify-content: center; gap: 0; border-radius: 8px; border-left-width: 0; }
}

/* モバイル：サイドバーをドロワー化 */
@media (max-width: 767px) {
  .menu-toggle { display: inline-flex; }
  .sidebar {
    position: fixed;
    left: 0; top: 0;
    width: 248px; flex-basis: 248px;
    height: 100vh;
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,.3);
  }
  .sidebar .brand-text, .sidebar .nav-label, .sidebar .nav-group-label { display: block; }
  .sidebar .sidebar-head { justify-content: space-between; }
  .sidebar .nav-item { justify-content: flex-start; gap: 11px; border-radius: 0 6px 6px 0; border-left-width: 3px; }
  .crm-shell.drawer-open .sidebar { transform: translateX(0); }
  .crm-shell.drawer-open .sidebar-overlay {
    display: block;
    position: fixed; inset: 0;
    background: rgba(30, 33, 36, .45);
    z-index: 25;
  }
  .content { padding: 16px; max-width: 100%; }
  .topbar { padding: 0 14px; }
  .topbar-right .root-status { display: none; }
  .form-grid, .form-grid.compact { grid-template-columns: 1fr; }
}
