:root {
  --primary:      #2b70ef;   /* primary-500 */
  --primary-50:   #f0f5ff;   /* primary-50 */
  --primary-hover:#2250df;   /* primary-600 */
  --accent:       #2C3E50;   /* PDF印刷・アクセント保持 */
  --accent-light: #34495E;
  --success:      #27AE60;
  --warning:      #F39C12;
  --danger:       #E74C3C;
  --bg:      #F8FAFC;   /* gray-50 */
  --surface: #FFFFFF;
  --border:  #E2E8F0;   /* slate-200 */
  --text:    #3d4b5f;   /* text-body */
  --text-muted: #888888;
  --sidebar-w: 220px;
  --radius: 12px;       /* rounded-xl */
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);  /* shadow-sm */
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { overflow-x: hidden; max-width: 100vw; }
body { font-family: 'Inter', 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', 'Noto Sans JP', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }
a { color: inherit; text-decoration: none; }
button { cursor: pointer; font-family: inherit; }

/* ---- Loading ---- */
.loading-screen { display: flex; align-items: center; justify-content: center; height: 100vh; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Top Loading Bar ---- */
#loading-bar {
  position: fixed; top: 0; left: 0; height: 3px; width: 0;
  background: var(--primary); z-index: 9999; opacity: 0;
}

/* ---- Auth ---- */
.auth-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--bg); }
.auth-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 40px 48px; width: 360px; }
.auth-logo { font-size: 22px; font-weight: 700; color: var(--accent); margin-bottom: 8px; letter-spacing: 1px; }
.auth-sub { font-size: 12px; color: var(--text-muted); margin-bottom: 28px; }
.auth-card h2 { font-size: 16px; margin-bottom: 20px; }
.auth-card .form-group { margin-bottom: 16px; }
.auth-card label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.auth-card input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 14px; transition: border-color .2s; }
.auth-card input:focus { outline: none; border-color: var(--primary); }
.auth-toggle { font-size: 12px; color: var(--text-muted); margin-top: 16px; text-align: center; }
.auth-toggle a { color: var(--primary); cursor: pointer; }

/* ---- Layout ---- */
.layout { display: flex; height: 100vh; overflow: hidden; }
.sidebar { width: var(--sidebar-w); background: var(--surface); border-right: 1px solid var(--border); color: var(--text); display: flex; flex-direction: column; flex-shrink: 0; height: 100vh; overflow: hidden; }
.sidebar-logo { padding: 24px 20px 16px; font-size: 18px; font-weight: 700; letter-spacing: 1px; color: var(--accent); border-bottom: 1px solid var(--border); }
.sidebar-logo span { font-size: 11px; font-weight: 400; color: var(--text-muted); display: block; margin-top: 2px; }
.profile-switcher { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.profile-switcher select { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 12px; cursor: pointer; }
.nav { flex: 1; padding: 8px 8px; overflow: hidden; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; font-size: 13px; font-weight: 500; cursor: pointer; transition: background .15s; color: var(--text); border-radius: 8px; }
.nav-item:hover { background: var(--bg); color: var(--text); }
.nav-item.active { background: var(--primary-50); color: var(--primary); font-weight: 600; }
.nav-item .icon { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }
.nav-section { padding: 16px 12px 4px; font-size: 10px; letter-spacing: 1px; text-transform: uppercase; color: var(--text-muted); }
.sidebar-footer { padding: 16px 20px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-muted); cursor: pointer; }
.sidebar-footer:hover { color: var(--text); }

.main { flex: 1; min-width: 0; overflow-x: hidden; overflow-y: auto; height: 100vh; }
.page { padding: 28px 32px; max-width: 1100px; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.page-title { font-size: 20px; font-weight: 700; color: var(--accent); }
.page-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

/* ---- Cards / Stats ---- */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-bottom: 28px; }
.stat-card { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-value { font-size: 24px; font-weight: 700; color: var(--accent); }
.stat-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.chart-wrap { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); margin-bottom: 28px; }
.chart-title { font-size: 14px; font-weight: 700; margin-bottom: 16px; }

/* ---- 案件一覧 階層アコーディオン ---- */
.acc-year { margin-bottom: 8px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.acc-year-hdr { background: var(--accent); color: #fff; padding: 8px 14px; cursor: pointer; font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.acc-month { border-top: 1px solid var(--border); }
.acc-month-hdr { background: var(--accent-light); color: #fff; padding: 6px 24px; cursor: pointer; font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 8px; }
.acc-cust { border-top: 1px solid var(--border); }
.acc-cust-hdr { background: var(--bg); padding: 6px 36px; cursor: pointer; font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--border); }
.acc-case { border-top: 1px solid var(--border); }
.acc-case-hdr { display: flex; align-items: center; gap: 10px; padding: 7px 12px 7px 56px; background: var(--surface); flex-wrap: wrap; }
.acc-case-hdr .case-title { flex: 0 0 auto; min-width: 140px; }
.acc-case-hdr .case-badges { flex: 1; }
.acc-case-hdr .case-total { flex: 0 0 auto; min-width: 130px; text-align: right; }
.acc-case-hdr .case-updated { flex: 0 0 auto; min-width: 80px; text-align: right; color: var(--text-muted); }
.acc-case-body { padding: 4px 12px 8px 56px; background: var(--bg); }
.acc-arrow { display: inline-block; transition: transform 0.2s; font-size: 11px; }
.customer-search-wrap { position: relative; }
.customer-search-wrap input[type="text"] { width: 100%; }
#customer-suggestions { display: none; position: absolute; z-index: 200; top: 100%; left: 0; right: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 4px; list-style: none; max-height: 200px; overflow-y: auto; box-shadow: var(--shadow); }
#customer-suggestions li { padding: 6px 10px; cursor: pointer; }
#customer-suggestions li:hover { background: var(--bg); }

/* ---- Table ---- */
.table-wrap { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 480px; }
thead th { background: var(--bg); padding: 10px 14px; font-size: 11px; color: var(--text-muted); text-align: left; font-weight: 500; border-bottom: 1px solid var(--border); text-transform: uppercase; letter-spacing: 0.05em; }
tbody tr { border-bottom: 1px solid var(--border); transition: background .1s; }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: #F8F9FD; }
tbody td { padding: 12px 14px; font-size: 13px; }

/* ---- Badges ---- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge-draft    { background: #F0F0F0; color: #666; }
.badge-issued   { background: #EBF5FB; color: #2980B9; }
.badge-paid     { background: #EAFAF1; color: #1E8449; }
.badge-cancelled{ background: #FDEDEC; color: #C0392B; }
.badge-estimate { background: #FEF9E7; color: #B7950B; }
.badge-invoice  { background: #EBF5FB; color: #1A5276; }
.badge-receipt  { background: #EAFAF1; color: #1E8449; }
.badge-order    { background: #F4ECF7; color: #76448A; }
.badge-order_ack{ background: #FDF2E9; color: #784212; }
.badge-delivery       { background: #E8F8F5; color: #1A7A5E; }
.badge-acceptance     { background: #EAF4FB; color: #154360; }
.badge-payment_notice { background: #F5EEF8; color: #6C3483; }
.status-select {
  border: none; padding: 2px 8px; border-radius: 20px;
  font-size: 11px; font-weight: 600; cursor: pointer;
  appearance: none; -webkit-appearance: none;
  min-width: 68px; text-align: center;
}
.status-select:focus { outline: 2px solid var(--primary); }

/* ---- Buttons ---- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 40px; padding: 0 16px; border-radius: 8px; font-size: 13px; font-weight: 600; border: none; transition: all .15s; white-space: nowrap; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-accent  { background: var(--accent); color: #fff; }
.btn-accent:hover  { background: var(--accent-light); }
.btn-outline { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-outline:hover { background: var(--bg); }
.btn-ghost   { background: transparent; color: var(--text); border: none; }
.btn-ghost:hover { background: var(--bg); }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-danger:hover  { background: #C0392B; }
.btn-success { background: var(--success); color: #fff; }
.btn-sm   { height: 32px; padding: 0 10px; font-size: 12px; }
.btn-icon { width: 32px; height: 32px; padding: 0; border-radius: 6px; }

/* ---- Forms ---- */
.form-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px; margin-bottom: 20px; }
.form-card h3 { font-size: 14px; font-weight: 700; color: var(--accent); margin-bottom: 16px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group.full { grid-column: 1 / -1; }
label { font-size: 12px; color: var(--text-muted); font-weight: 500; }
input, select, textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; font-family: inherit; transition: border-color .2s; background: var(--surface);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
textarea { resize: vertical; min-height: 60px; }

/* ---- Lines Editor ---- */
.lines-editor { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 16px; }
.lines-toolbar { display: flex; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--bg); flex-wrap: wrap; }
.lines-table-wrap { overflow-x: auto; }
.lines-table { width: 100%; border-collapse: collapse; }
.lines-table th { background: var(--bg); padding: 8px 10px; font-size: 11px; color: var(--text-muted); font-weight: 600; border-bottom: 1px solid var(--border); text-align: left; }
.lines-table td { padding: 6px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.lines-table tr:last-child td { border-bottom: none; }
.lines-table input, .lines-table select { padding: 5px 7px; font-size: 12px; border-radius: 4px; }
.line-header-row td { background: #F0F4FF; font-weight: 600; }
.line-sub-row td:first-child { padding-left: 28px; }
.line-drag { cursor: grab; color: var(--text-muted); padding: 0 4px; }
.lines-total { display: flex; justify-content: flex-end; padding: 12px 16px; gap: 32px; font-size: 13px; border-top: 1px solid var(--border); background: var(--bg); }
.lines-total .total-row { display: flex; gap: 12px; align-items: center; }
.lines-total .total-label { color: var(--text-muted); }
.lines-total .grand-total { font-size: 18px; font-weight: 700; color: var(--accent); }

/* ---- Modal ---- */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: 0 8px 40px rgba(0,0,0,.18); width: 560px; max-width: 96vw; max-height: 90vh; overflow-y: auto; }
.modal-header { padding: 20px 24px 14px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-body { padding: 20px 24px; }
.modal-footer { padding: 14px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* ---- Misc ---- */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
.search-input { padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; width: 220px; }
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .icon { font-size: 40px; margin-bottom: 12px; }
.actions { display: flex; gap: 6px; }
.text-right { text-align: right; }
.text-muted { color: var(--text-muted); }
.mt-2 { margin-top: 8px; }
.mb-3 { margin-bottom: 12px; }
.alert { display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
.alert-danger  { background: #FEF2F2; color: #991B1B; border: 1px solid #FECACA; }
.alert-success { background: #ECFDF5; color: #065F46; border: 1px solid #A7F3D0; }
.alert-warning { background: #FFFBEB; color: #92400E; border: 1px solid #FDE68A; }
.alert-info    { background: var(--primary-50); color: #1E3A8A; border: 1px solid #BFDBFE; }

/* ---- Lines drag & drop ---- */
.line-drag { cursor: grab; user-select: none; color: var(--text-muted); padding: 0 6px !important; }
.line-drag:active { cursor: grabbing; }
tr.dragging { opacity: 0.4; }
tr.drag-over { outline: 2px solid var(--primary); outline-offset: -1px; }

/* ---- Hamburger ---- */
#hamburger { display: none; position: fixed; top: 10px; left: 10px; z-index: 300; background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 8px 10px; font-size: 18px; line-height: 1; cursor: pointer; }
#sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 199; }

/* ---- Responsive ---- */
@media (max-width: 900px) {
  #hamburger { display: block; }
  .sidebar { position: fixed; left: -240px; top: 0; bottom: 0; width: 220px; z-index: 200; transition: left .25s; }
  .sidebar.open { left: 0; }
  #sidebar-overlay.open { display: block; }
  .layout { display: block; }
  .main { padding-top: 0; }
  .page { padding: 56px 14px 20px; }
  .page-header { flex-direction: column; align-items: flex-start; gap: 10px; }
  .page-header > div:last-child { flex-wrap: wrap; gap: 6px; }
  .toolbar { gap: 6px; }
  .search-input { width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .acc-case-hdr { padding-left: 36px; flex-wrap: wrap; }
  .acc-case-body { padding-left: 36px; }
  .acc-cust-hdr { padding-left: 24px; }
  .acc-month-hdr { padding-left: 16px; }
  .modal { width: 96vw; }
  .actions { gap: 4px; }
  .btn-sm { padding: 4px 7px; font-size: 11px; }

  /* テーブル横スクロール */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { min-width: 520px; }

  /* 案件一覧：原価・利益列を非表示 */
  .hide-sp { display: none; }

  /* 明細テーブル横スクロール */
  .lines-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .lines-table { min-width: 560px; font-size: 11px; }
  .lines-table th, .lines-table td { padding: 4px 4px; }

  /* iOS Safari: font-size < 16px の input にフォーカスすると自動ズームするのを防ぐ */
  input, select, textarea,
  .lines-table input, .lines-table select,
  .search-input, #markup-rate { font-size: 16px !important; }
}

/* ============================================================
   モバイル専用 UI (max-width: 640px)
   ============================================================ */

/* ---- Bottom Nav ---- */
#bottom-nav {
  display: none;
  position: fixed; bottom: 0; left: 0; right: 0; height: 58px;
  background: var(--accent); z-index: 201;
  border-top: 1px solid rgba(255,255,255,.12);
  box-shadow: 0 -2px 8px rgba(0,0,0,.15);
}
.bn-item {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: rgba(255,255,255,.6); font-size: 10px; gap: 3px;
  cursor: pointer; padding: 4px 0;
  -webkit-tap-highlight-color: transparent; transition: color .12s;
}
.bn-item .bn-icon { font-size: 20px; line-height: 1; }
.bn-item.active { color: #fff; font-weight: 600; }
.bn-item.bn-new .bn-icon {
  background: rgba(255,255,255,.2); border-radius: 50%;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700;
}
.bn-item.bn-new.active .bn-icon { background: rgba(255,255,255,.35); }

/* ---- Document Cards ---- */
.doc-card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 10px;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  active-transition: box-shadow .1s;
}
.doc-card:active { box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.doc-card-hdr { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.doc-card-num { font-size: 12px; font-weight: 700; color: var(--primary); margin-bottom: 3px; }
.doc-card-customer { font-size: 14px; font-weight: 600; color: var(--text); }
.doc-card-ttl { font-size: 12px; color: var(--text-muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border); }
.doc-card-amount { font-size: 16px; font-weight: 700; color: var(--accent); }
.doc-card-date { font-size: 11px; color: var(--text-muted); }

/* モバイルのみ表示/非表示 */
.show-sp { display: none; }

@media (max-width: 640px) {
  #hamburger { display: none; }
  #bottom-nav { display: flex; }
  .page { padding: 12px 14px 70px !important; }
  .show-sp { display: block; }

  /* ダッシュボード年別グリッド → 縦積み */
  #dash-year-grid { grid-template-columns: 1fr !important; }

  /* 最近の書類 非表示 */
  .dash-recent { display: none; }
}
