:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --secondary: #64748b;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --bg: #f1f5f9;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --sidebar-w: 260px;
}

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

body {
  font-family: 'Segoe UI', 'Noto Sans JP', sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: #1e293b;
  color: #cbd5e1;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

#sidebar .logo {
  padding: 20px 16px 16px;
  border-bottom: 1px solid #334155;
}

#sidebar .logo h1 {
  font-size: 14px;
  font-weight: 700;
  color: #f8fafc;
  line-height: 1.4;
}

#sidebar .logo p {
  font-size: 11px;
  color: #94a3b8;
  margin-top: 4px;
}

#sidebar nav { flex: 1; padding: 12px 0; }

#sidebar nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  color: #94a3b8;
  text-decoration: none;
  font-size: 13px;
  border-radius: 0;
  transition: all .15s;
  cursor: pointer;
}

#sidebar nav a:hover,
#sidebar nav a.active {
  background: #334155;
  color: #f8fafc;
}

#sidebar nav a .icon { font-size: 16px; width: 20px; text-align: center; }

.nav-section-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: #475569;
  padding: 16px 16px 6px;
}

/* ── Main ────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#topbar {
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

#topbar h2 { font-size: 16px; font-weight: 600; }

#content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

/* ── Cards ───────────────────────────────────────────── */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 16px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.card-header h3 { font-size: 14px; font-weight: 600; }

/* ── Grid ────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }

/* ── Forms ───────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 12px; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; }
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
  background: #fff;
  transition: border-color .15s;
  font-family: inherit;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.form-group textarea { resize: vertical; min-height: 140px; }

/* ── Buttons ─────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all .15s;
  text-decoration: none;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: #f1f5f9; color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: #e2e8f0; }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #15803d; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-warning:hover:not(:disabled) { background: #b45309; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-ghost { background: none; border: none; color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); background: var(--bg); }

/* ── Badge / Level ───────────────────────────────────── */
.level-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 700;
}
.level-high { background: #dcfce7; color: #15803d; }
.level-mid  { background: #fef9c3; color: #854d0e; }
.level-low  { background: #fee2e2; color: #b91c1c; }
.level-none { background: #f1f5f9; color: #94a3b8; }

/* ── Frame List ──────────────────────────────────────── */
.frame-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 10px;
  background: var(--card-bg);
  cursor: pointer;
  transition: all .15s;
}
.frame-item:hover { border-color: var(--primary); box-shadow: 0 2px 8px rgba(37,99,235,.1); }
.frame-item .frame-icon { font-size: 24px; }
.frame-item .frame-info { flex: 1; min-width: 0; }
.frame-item .frame-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.frame-item .frame-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.frame-item .frame-actions { display: flex; gap: 6px; opacity: 0; transition: opacity .15s; }
.frame-item:hover .frame-actions { opacity: 1; }

/* ── Section Tree ────────────────────────────────────── */
.section-tree { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.section-node {
  border-bottom: 1px solid var(--border);
}
.section-node:last-child { border-bottom: none; }
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--card-bg);
  cursor: pointer;
  transition: background .15s;
}
.section-header:hover { background: #f8fafc; }
.section-header.level-1 { background: #f8fafc; font-weight: 600; }
.section-header.level-2 { padding-left: 32px; }
.section-header.level-3 { padding-left: 48px; }
.section-header.level-4 { padding-left: 64px; }
.section-header.level-5 { padding-left: 80px; }
.section-heading { flex: 1; font-size: 13px; }
.section-body {
  display: none;
  padding: 12px 16px 16px;
  background: #fafafa;
  border-top: 1px dashed var(--border);
}
.section-body.open { display: block; }
.section-text {
  font-size: 12px;
  line-height: 1.7;
  white-space: pre-wrap;
  color: var(--text);
}
.section-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 10px;
}
.section-tab {
  padding: 4px 12px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text-muted);
}
.section-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* ── Stats ───────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}
.stat-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  text-align: center;
}
.stat-card .stat-value { font-size: 28px; font-weight: 700; color: var(--primary); }
.stat-card .stat-label { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

/* ── Toast ───────────────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: #1e293b;
  color: #f8fafc;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  max-width: 340px;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  animation: slideIn .2s ease;
}
.toast.success { border-left: 4px solid var(--success); }
.toast.error { border-left: 4px solid var(--danger); }
.toast.info { border-left: 4px solid var(--primary); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

/* ── Loading ─────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 12px;
  color: #fff;
  font-size: 14px;
}
.loading-overlay .big-spinner {
  width: 40px;
  height: 40px;
  border-width: 4px;
}

/* ── Modal ───────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 8000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal h3 { font-size: 16px; font-weight: 700; margin-bottom: 16px; }

/* ── Table ───────────────────────────────────────────── */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; }
.data-table th { background: #f8fafc; font-weight: 600; font-size: 12px; color: var(--text-muted); }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #f8fafc; }

/* ── Rewrite Preview ─────────────────────────────────── */
.rw-preview-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.rw-preview-alert {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--danger);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 12px;
}
.rw-preview-row--failed td { background: #fef2f2; }
.rw-status-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 4px;
}
.rw-status-badge--changed { background: #dcfce7; color: var(--success); }
.rw-status-badge--failed { background: #fee2e2; color: var(--danger); }
.rw-diff-details summary {
  cursor: pointer;
  user-select: none;
  list-style: none;
}
.rw-diff-details summary::-webkit-details-marker { display: none; }
.rw-diff-summary {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 13px;
}
.rw-diff-summary-heading { font-weight: 600; flex: 1; min-width: 120px; }
.rw-diff-summary-meta {
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.rw-diff-summary-toggle {
  font-size: 12px;
  color: var(--primary);
  white-space: nowrap;
}
.rw-diff-body { margin-top: 10px; }
.rw-diff-compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  align-items: stretch;
}
.rw-diff-pane {
  border-radius: 6px;
  padding: 8px 10px;
  min-width: 0;
}
.rw-diff-pane--before {
  border-left: 3px solid var(--border);
  background: #f8fafc;
}
.rw-diff-pane--after {
  border-left: 3px solid var(--success);
  background: #f0fdf4;
}
.rw-diff-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.rw-diff-pane-body { min-height: 2em; }
.rw-diff-empty {
  font-size: 12px;
  color: var(--text-muted);
}
.rw-diff-text {
  font-family: inherit;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 240px;
  overflow-y: auto;
  margin: 0;
}
.rw-diff-textarea {
  width: 100%;
  box-sizing: border-box;
  font-family: inherit;
  font-size: 12px;
  line-height: 1.6;
  min-height: 120px;
  max-height: 240px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  resize: vertical;
}
.rw-diff-pane--after .rw-diff-textarea {
  border-color: var(--primary);
  background: #f8fafc;
}
.rw-error-msg {
  font-size: 12px;
  color: var(--danger);
  margin-bottom: 8px;
}
@media (max-width: 768px) {
  .rw-diff-compare { grid-template-columns: 1fr; }
}

/* ── Process Progress ─────────────────────────────────── */
.process-log {
  background: #0f172a;
  color: #94a3b8;
  border-radius: 8px;
  padding: 16px;
  font-family: monospace;
  font-size: 12px;
  line-height: 1.7;
  max-height: 240px;
  overflow-y: auto;
}
.process-log .log-ok { color: #4ade80; }
.process-log .log-err { color: #f87171; }
.process-log .log-info { color: #60a5fa; }

/* ── Responsive ──────────────────────────────────────── */
@media (max-width: 768px) {
  :root { --sidebar-w: 0; }
  #sidebar { display: none; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* ── Auth Overlay ────────────────────────────────────── */
.hidden { display: none !important; }

#main-app {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a5f 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.auth-card {
  background: var(--card-bg);
  border-radius: 16px;
  padding: 40px 36px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.4);
}

.auth-card__logo {
  text-align: center;
  margin-bottom: 28px;
}

.auth-card__icon {
  font-size: 48px;
  display: block;
  margin-bottom: 10px;
}

.auth-card__logo h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.auth-card__logo p {
  font-size: 12px;
  color: var(--text-muted);
}

.auth-card__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 10px;
}

.auth-card__desc {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.6;
  margin-bottom: 20px;
}

.auth-card__hint {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  margin-top: 12px;
}

.auth-btn {
  width: 100%;
  padding: 13px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
  letter-spacing: .02em;
}

.auth-btn:hover:not(:disabled) { background: var(--primary-hover); }
.auth-btn:disabled { opacity: .6; cursor: not-allowed; }

.auth-message {
  margin-top: 14px;
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13px;
  text-align: center;
}

.auth-message--error {
  background: #fef2f2;
  color: var(--danger);
  border: 1px solid #fecaca;
}
