*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #ffffff;
  --bg2:       #f6f6f6;
  --border:    #dddddd;
  --text:      #111111;
  --text2:     #555555;
  --accent:    #1a3a8c;
  --accent-h:  #142d6e;
  --danger:    #8c1a1a;
  --ok:        #1a5c2a;
  --sidebar-w: 220px;
}

body {
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", sans-serif;
  font-size: 14px;
  line-height: 1.65;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ── レイアウト ────────────────────────────────────────── */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* ── サイドバー ─────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: #fafafa;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  overflow-y: auto;
  z-index: 100;
}
.sidebar-brand {
  padding: 20px 18px 16px;
  font-size: 14px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  line-height: 1.3;
}
.sidebar-brand small {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--text2);
  margin-top: 3px;
}
.sidebar nav { padding: 8px 0; flex: 1; }
.sidebar nav a {
  display: block;
  padding: 9px 18px;
  color: var(--text);
  text-decoration: none;
  font-size: 13.5px;
}
.sidebar nav a:hover { background: #eeeeee; }
.sidebar nav a.active { font-weight: 700; background: #e8ecf8; color: var(--accent); }
.sidebar nav a.nav-sub { padding-left: 30px; font-size: 13px; color: var(--text2); }
.sidebar nav a.nav-sub.active { color: var(--accent); }

/* ── 折りたたみグループ ──────────────────────────────────── */
.sidebar nav .nav-group { user-select: none; }
.sidebar nav .nav-group-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 9px 18px;
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 13.5px;
  font-family: inherit;
}
.sidebar nav .nav-group-toggle:hover { background: #eeeeee; }
.sidebar nav .nav-group.has-active > .nav-group-toggle { color: var(--accent); font-weight: 600; }
.sidebar nav .nav-group .caret {
  font-size: 10px;
  color: var(--text2);
  transition: transform .15s;
  margin-left: 6px;
}
.sidebar nav .nav-group.open .caret { transform: rotate(180deg); }
.sidebar nav .nav-group-items {
  max-height: 0;
  overflow: hidden;
  transition: max-height .2s ease-out;
}
.sidebar nav .nav-group.open .nav-group-items { max-height: 400px; }
.sidebar-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text2);
}
.sidebar-footer a { color: var(--text2); text-decoration: none; }
.sidebar-footer a:hover { color: var(--text); }

/* ── メインエリア ─────────────────────────────────────────── */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 32px 40px;
  max-width: 960px;
}

h1 { font-size: 22px; font-weight: 700; margin-bottom: 20px; }
h2 { font-size: 17px; font-weight: 700; margin: 28px 0 12px;
     padding-bottom: 7px; border-bottom: 2px solid var(--text); }
h3 { font-size: 15px; font-weight: 700; margin: 20px 0 8px; }

p { margin-bottom: 12px; }

/* ── フォーム ─────────────────────────────────────────────── */
.form-row { margin-bottom: 16px; }
label { display: block; font-size: 12px; color: var(--text2); margin-bottom: 5px; }
input[type=text], input[type=email], input[type=password], select, textarea {
  width: 100%; padding: 9px 12px;
  border: 1px solid var(--border); border-radius: 4px;
  font-size: 14px; color: var(--text); background: var(--bg);
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }

/* ── ボタン ──────────────────────────────────────────────── */
.btn {
  display: inline-block;
  padding: 9px 20px;
  border: none; border-radius: 4px;
  font-size: 14px; font-weight: 600;
  cursor: pointer; text-decoration: none;
  line-height: 1;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-h); }
.btn-outline {
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent);
}
.btn-outline:hover { background: var(--accent); color: #fff; }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #6e1212; }
.btn[disabled], .btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── カード ──────────────────────────────────────────────── */
.card {
  border: 1px solid var(--border);
  padding: 20px 24px;
  margin-bottom: 20px;
  background: var(--bg);
}
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.card h4 { font-size: 13px; color: var(--text2); margin-bottom: 6px; }
.card .value { font-size: 22px; font-weight: 700; }
.card .sub   { font-size: 12px; color: var(--text2); margin-top: 4px; }

/* ── テーブル ────────────────────────────────────────────── */
table { width: 100%; border-collapse: collapse; font-size: 13px; margin: 12px 0; }
th { background: var(--text); color: #fff; text-align: left;
     padding: 8px 12px; font-weight: 700; }
td { padding: 7px 12px; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }

/* ── バッジ ──────────────────────────────────────────────── */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 3px;
  font-size: 11px; font-weight: 700;
}
.badge-ok     { background: #d4edda; color: var(--ok); }
.badge-warn   { background: #fff3cd; color: #7d5a00; }
.badge-danger { background: #f8d7da; color: var(--danger); }
.badge-gray   { background: #e9ecef; color: #555; }

/* ── メッセージ ──────────────────────────────────────────── */
.msg {
  padding: 10px 14px; border-radius: 4px;
  font-size: 13px; margin: 12px 0; display: none;
}
.msg.err { background: #fce4e4; color: var(--danger); display: block; border-left: 3px solid var(--danger); }
.msg.ok  { background: #e8f5e9; color: var(--ok);     display: block; border-left: 3px solid var(--ok); }
.msg.info{ background: #e8ecf8; color: var(--accent);  display: block; border-left: 3px solid var(--accent); }

/* ── プログレスバー ──────────────────────────────────────── */
.progress-wrap { background: #e0e0e0; border-radius: 3px; height: 8px; margin: 8px 0; overflow: hidden; }
.progress-bar  { height: 100%; background: var(--accent); transition: width 0.3s; }

/* ── ドロップゾーン ──────────────────────────────────────── */
.drop-zone {
  border: 2px dashed var(--border); border-radius: 6px;
  padding: 40px; text-align: center; cursor: pointer;
  color: var(--text2); transition: border-color 0.2s;
}
.drop-zone.over, .drop-zone:hover { border-color: var(--accent); color: var(--accent); }
.drop-zone input[type=file] { display: none; }

/* ── ユーティリティ ──────────────────────────────────────── */
.mt-8  { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.mb-0  { margin-bottom: 0; }
.text-muted { color: var(--text2); }
.text-small { font-size: 12px; }
.text-right { text-align: right; }
.flex-row   { display: flex; align-items: center; gap: 12px; }
.flex-wrap  { flex-wrap: wrap; }
hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

/* ── ログイン・登録ページ専用 ────────────────────────────── */
.auth-wrap {
  min-height: 100vh; display: flex;
  justify-content: center; align-items: center;
  background: var(--bg2); padding: 24px;
}
.auth-box {
  background: var(--bg); border: 1px solid var(--border);
  padding: 40px 36px; width: 100%; max-width: 400px;
}
.auth-box h1 { font-size: 18px; text-align: center; margin-bottom: 6px; }
.auth-box .sub { text-align: center; color: var(--text2); font-size: 13px; margin-bottom: 24px; }
.auth-box .btn { width: 100%; padding: 12px; font-size: 15px; text-align: center; }

/* ── ドロップエリア ───────────────────────────────────────── */
.drop-area {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 32px 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  margin-top: 12px;
}
.drop-area:hover, .drop-area.drag-over {
  border-color: var(--accent);
  background: #f0f4ff;
}
.drop-inner { pointer-events: none; }

/* ── 共通アップローダ (file_uploader.js) ───────────────────── */
.fu-drop {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 28px 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
}
.fu-drop:hover, .fu-drop.fu-drag {
  border-color: var(--accent);
  background: #f0f4ff;
}
.fu-inner { pointer-events: none; }
.fu-icon { font-size: 40px; color: var(--text2); }
.fu-label { margin-top: 8px; }
.fu-sublabel { font-size: 12px; color: var(--text2); margin-top: 4px; }
.fu-msg { margin-top: 8px; }
.fu-progress { margin-top: 8px; }
.fu-progress-text { font-size: 13px; color: var(--text2); }
.fu-progress-bar-bg {
  background: var(--border);
  border-radius: 4px;
  height: 6px;
  margin-top: 6px;
  overflow: hidden;
}
.fu-progress-bar {
  background: var(--accent);
  height: 6px;
  border-radius: 4px;
  width: 0%;
  transition: width .3s;
}
.fu-controls { display: flex; gap: 8px; margin-top: 8px; }
.fu-controls .fu-btn { font-size: 12px; }
.fu-results { margin-top: 16px; font-family: monospace; }
.fu-result { font-size: 13px; padding: 4px 0; }
.fu-result.ok { color: var(--ok); }
.fu-result.ng { color: var(--danger); }
.fu-actions { display: flex; gap: 10px; margin-top: 10px; }
.fu-btn {
  padding: 4px 10px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
}
.fu-btn:hover { background: #eef0f6; }
.fu-cancel { color: var(--danger); border-color: var(--danger); }

/* ── select（複数選択） ──────────────────────────────────── */
.form-select {
  display: block;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  font-size: 14px;
  color: var(--text);
  margin-top: 4px;
}
.form-select:focus { outline: none; border-color: var(--accent); }

/* ── データテーブル（リサイズ可・ページング付き） ─────────── */
.dv-wrap {
  position: relative;
  border: 1px solid var(--border);
  background: var(--bg);
  resize: both;
  overflow: hidden;
  min-width: 320px;
  min-height: 240px;
  width: 100%;
  height: 600px;
  border-radius: 4px;
}
.dv-scroll {
  width: 100%;
  height: calc(100% - 44px);
  overflow: auto;
}
.dv-table {
  border-collapse: separate;
  border-spacing: 0;
  font-size: 12.5px;
  table-layout: fixed;
  width: max-content;
}
.dv-table th, .dv-table td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 5px 8px;
  background: var(--bg);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: middle;
}
.dv-table thead th {
  position: sticky;
  top: 0;
  background: var(--bg2);
  font-weight: 600;
  border-top: 1px solid var(--border);
  user-select: none;
  z-index: 2;
}
.dv-table thead th:first-child,
.dv-table tbody td:first-child {
  position: sticky;
  left: 0;
  z-index: 3;
  border-left: 1px solid var(--border);
}
.dv-table thead th:first-child { z-index: 4; background: var(--bg2); }
.dv-table tbody tr:nth-child(even) td { background: #fafafa; }
.dv-table tbody tr:hover td { background: #f0f3fa; }
.dv-table th.dv-num,
.dv-table td.dv-num { text-align: right; font-variant-numeric: tabular-nums; }
.dv-col-resizer {
  position: absolute;
  right: 0;
  top: 0;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
}
.dv-col-resizer:hover { background: rgba(26,58,140,0.2); }
.dv-th-inner { position: relative; padding-right: 6px; }

.dv-pager {
  height: 44px;
  border-top: 1px solid var(--border);
  background: var(--bg2);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  font-size: 12px;
}
.dv-pager button {
  padding: 4px 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text);
}
.dv-pager button:hover:not(:disabled) { background: #eef0f6; }
.dv-pager button:disabled { opacity: .4; cursor: not-allowed; }
.dv-pager input[type=number] {
  width: 64px;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 3px;
  font-size: 12px;
  text-align: right;
}
.dv-pager .dv-spacer { flex: 1; }
.dv-pager .dv-info { color: var(--text2); }

/* ── リサイズ可能なチャートコンテナ (chart_resizer.js と併用) ─ */
.chart-resizable {
  position: relative;
  resize: both;
  overflow: auto;
  min-width: 320px;
  min-height: 240px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  /* 右下角のリサイズハンドルを目立たせる */
  background-image: linear-gradient(135deg, transparent 0 calc(100% - 14px),
                                            var(--text2) calc(100% - 14px) calc(100% - 12px),
                                            transparent calc(100% - 12px) calc(100% - 8px),
                                            var(--text2) calc(100% - 8px) calc(100% - 6px),
                                            transparent calc(100% - 6px) 100%);
  background-repeat: no-repeat;
  background-position: bottom right;
  background-size: 16px 16px;
}

/* 名寄せページ用 */
.nm-controls {
  display: flex;
  align-items: end;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.nm-slider-wrap { min-width: 220px; }
.nm-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.nm-toggle button {
  padding: 6px 14px;
  background: var(--bg);
  border: none;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.nm-toggle button.active {
  background: var(--accent);
  color: #fff;
}
.nm-cand-list { display: flex; flex-wrap: wrap; gap: 6px; }
.nm-cand {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 12px;
  background: var(--bg);
  cursor: pointer;
  user-select: none;
}
.nm-cand:hover { background: #eef0f6; }
.nm-cand.selected {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.nm-cand .nm-sim {
  margin-left: 5px;
  opacity: .75;
  font-size: 11px;
}
.nm-cand.saved::before {
  content: "✓";
  color: var(--ok);
  margin-right: 4px;
  font-weight: 700;
}
.nm-cand.selected.saved::before { color: #cfe7d4; }

/* 名寄せマトリクス */
.nm-matrix-wrap {
  position: relative;
  border: 1px solid var(--border);
  background: var(--bg);
  overflow: auto;
  max-height: 720px;
  resize: both;
  min-width: 400px;
  min-height: 320px;
}
.nm-matrix {
  border-collapse: separate;
  border-spacing: 0;
  font-size: 11px;
}
.nm-matrix th, .nm-matrix td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0;
  text-align: center;
  background: var(--bg);
}
.nm-matrix thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: #ffffff;
  color: var(--text);
  font-weight: 700;
  vertical-align: bottom;
  padding: 4px 0;
  height: 260px;
  min-width: 26px;
  max-width: 26px;
  border-bottom: 2px solid var(--accent);
  overflow: visible;
}
.nm-matrix thead th.nm-corner {
  z-index: 5;
  left: 0;
  position: sticky;
  background: var(--accent);
  color: #ffffff;
  min-width: 280px;
  max-width: 280px;
  text-align: left;
  padding: 8px;
  font-size: 12px;
  font-weight: 700;
  height: 260px;
  border-bottom: 2px solid var(--accent);
}
/* 列ヘッダの機種名: 横書きのまま反時計回りに90°回転（下→上で読める）
   writing-mode: sideways-lr で transform を使わずに正しい位置に配置 */
.nm-matrix thead th .nm-rot {
  writing-mode: sideways-lr;
  display: inline-block;
  white-space: nowrap;
  font-weight: 600;
  font-size: 12px;
  line-height: 26px;             /* 列幅と同じにして水平方向で中央寄せ */
  color: var(--text);
  text-align: left;              /* sideways-lr では下端寄せ */
  max-height: 240px;
  overflow: hidden;
}
.nm-matrix tbody th {
  position: sticky;
  left: 0;
  z-index: 2;
  background: #ffffff;
  color: var(--text);
  text-align: left;
  padding: 4px 8px;
  font-weight: 600;
  font-size: 12px;
  min-width: 280px;
  max-width: 280px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-right: 2px solid var(--accent);
}
.nm-matrix tbody tr:nth-child(even) th { background: #f0f0f0; }
.nm-matrix tbody tr:nth-child(even) td { background: #f5f5f5; }
.nm-matrix tbody tr:hover th { background: #d8e1f2 !important; color: var(--text); }
.nm-matrix tbody tr:hover td { background: #e8edf7; }
.nm-cell {
  width: 26px;
  height: 26px;
  min-width: 26px;
  cursor: pointer;
  user-select: none;
  position: relative;
}
.nm-cell.has-sim {
  background: var(--sim-bg, transparent) !important;
}
.nm-cell.checked {
  background: var(--accent) !important;
  color: #fff;
}
.nm-cell.checked::before {
  content: '✓';
  font-size: 13px;
  color: #fff;
  font-weight: 700;
  position: relative;
  z-index: 1;
}
.nm-matrix-stats {
  font-size: 12px;
  color: var(--text2);
  padding: 6px 0;
}

/* ── 共通: 右下ドラッグでサイズ変更可能なコンテナ ────────── */
.resizable-wrap {
  position: relative;
  resize: both;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 4px;
  min-width: 320px;
  min-height: 240px;
  width: 100%;
}
.resizable-wrap > * {
  width: 100%;
  height: 100%;
}
/* リサイズハンドルが他要素に隠れないように右下にビジュアル目印 */
.resizable-wrap::after {
  content: '';
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 14px;
  height: 14px;
  background:
    linear-gradient(135deg, transparent 0 50%, var(--text2) 50% 60%, transparent 60% 70%, var(--text2) 70% 80%, transparent 80% 100%);
  pointer-events: none;
  opacity: 0.6;
}

/* リサイズ可能なテーブル + 列幅リサイズ */
.rtable-wrap {
  position: relative;
  resize: both;
  overflow: auto;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 4px;
  min-width: 320px;
  min-height: 200px;
  width: 100%;
}
.rtable {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  width: max-content;
  font-size: 12.5px;
}
.rtable th, .rtable td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 5px 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--bg);
}
.rtable thead th {
  position: sticky;
  top: 0;
  background: #1a3a8c;     /* アクセント色（紺） */
  color: #ffffff;
  font-weight: 700;
  user-select: none;
  z-index: 2;
}
.rtable thead th .col-resizer:hover { background: rgba(255,255,255,0.35); }
.rtable tbody tr:nth-child(even) td { background: #fafafa; }
.rtable tbody tr:hover td { background: #f0f3fa; }
.rtable .col-resizer {
  position: absolute;
  right: 0;
  top: 0;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
}
.rtable .col-resizer:hover { background: rgba(26,58,140,0.25); }
.rtable th .th-inner { position: relative; padding-right: 6px; }

/* ── レスポンシブ ─────────────────────────────────────────── */
@media (max-width: 680px) {
  .sidebar { display: none; }
  .main { margin-left: 0; padding: 20px 16px; }
  .card-grid { grid-template-columns: 1fr; }
  .dv-wrap { height: 480px; }
}
