:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --border: #2a2d3e;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f52d4;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #22c55e;
  --info: #3b82f6;
  --radius: 8px;
}

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

html {
  overflow-x: clip;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.6;
  min-width: 0;
}

/* Navbar */
.navbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 100;
  /* sticky要素自体は幅を固定 */
  left: 0;
  right: 0;
}
.nav-brand { font-size: 18px; font-weight: 700; color: var(--primary); white-space: nowrap; min-width: 0; }
.nav-links { display: flex; flex-wrap: wrap; gap: 4px; min-width: 0; }
.nav-links a {
  color: var(--text-muted);
  text-decoration: none;
  padding: 4px 10px;
  border-radius: 4px;
  transition: color .2s;
  white-space: nowrap;
}
.nav-links a:hover { color: var(--text); }

/* Container */
.container { max-width: 960px; margin: 0 auto; padding: 24px 16px; }

/* Hero */
.hero { text-align: center; padding: 64px 0; }
.hero h1 { font-size: 40px; font-weight: 800; color: var(--primary); margin-bottom: 12px; }
.hero p { color: var(--text-muted); font-size: 16px; margin-bottom: 32px; }
.hero-actions { display: flex; gap: 12px; justify-content: center; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 24px;
}
.card h3 { font-size: 16px; font-weight: 600; margin-bottom: 16px; color: var(--text); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  transition: background .2s, opacity .2s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #3a3d4e; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* Forms */
.form-row {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: var(--text-muted);
  font-size: 12px;
  flex: 1 1 140px;
  min-width: 0;
}
input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 14px;
  width: 100%;
  min-width: 0;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
}
.checkbox-label {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  flex: 0 1 auto;
}
.checkbox-label input[type="checkbox"] { width: auto; }

/* Table */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.table th, .table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.table th { color: var(--text-muted); font-weight: 500; }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: rgba(255,255,255,.02); }

/* Ranking */
.ranking-toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.ranking-table { table-layout: auto; width: 100%; min-width: 360px; }
.ranking-table th, .ranking-table td { padding: 10px 8px; }
.ranking-table th:first-child, .ranking-table td:first-child { padding-left: 4px; padding-right: 4px; width: 32px; }
.ranking-table .domain-cell { font-family: monospace; word-break: break-all; }
.domain-link { color: var(--text); text-decoration: none; }
.domain-link:hover { color: var(--primary); text-decoration: underline; }
.ranking-table .gmail-cell { width: 52px; padding: 10px 4px; text-align: center; }
.gmail-link {
  font-size: 11px;
  color: var(--text-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 5px;
  white-space: nowrap;
}
.gmail-link:hover { color: var(--text); border-color: var(--text-muted); }
.ranking-table .count-cell { text-align: right; color: var(--text-muted); white-space: nowrap; width: 60px; }
.ranking-table .bar-cell { padding: 10px 4px; width: 80px; }
.bar { height: 8px; background: var(--primary); border-radius: 4px; min-width: 2px; transition: width .3s; }

/* Row inline status */
.row-state-th   { width: 120px; }
.row-state-cell { font-size: 12px; white-space: nowrap; }
.row-status-pending { color: var(--text-muted); }
.row-status-running { color: var(--info); }
.row-status-warn    { color: #f59e0b; }
.row-status-error   { color: var(--danger); }

/* Delete summary */
.delete-summary {
  margin-bottom: 16px;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 8px;
  overflow: hidden;
  font-size: 13px;
}
.summary-header {
  padding: 10px 14px;
  background: var(--surface, #f8fafc);
  border-bottom: 1px solid var(--border, #e2e8f0);
  font-weight: 600;
}
.summary-total strong { font-size: 15px; }
.summary-table { width: 100%; border-collapse: collapse; }
.summary-table td { padding: 6px 14px; vertical-align: top; border-bottom: 1px solid var(--border, #e2e8f0); }
.summary-table tr:last-child td { border-bottom: none; }
.summary-domain { font-weight: 500; white-space: nowrap; width: 200px; }
.summary-count  { white-space: nowrap; width: 150px; }
.summary-detail { color: var(--text-muted); }
.summary-af     { display: inline-block; margin-right: 10px; font-size: 12px; }
.summary-row-ok   .summary-count { color: var(--success, #16a34a); }
.summary-row-warn .summary-count { color: #f59e0b; }
.summary-row-error .summary-count { color: var(--danger); }
.summary-skip { padding: 6px 14px; margin: 0; color: var(--text-muted); font-size: 12px; }

/* Delete progress bar */
.delete-progress-wrap {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-bottom: 14px;
}
.delete-progress-bar-track {
  flex: 1;
  height: 10px;
  background: var(--border, #e2e8f0);
  border-radius: 5px;
  overflow: hidden;
}
.delete-progress-bar {
  height: 100%;
  background: var(--primary);
  border-radius: 5px;
  transition: width .4s ease;
}
.delete-progress-label {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
}
.delete-current {
  width: 100%;
  font-size: 12px;
  color: var(--info);
  margin-top: 4px;
  min-height: 1.4em;
  font-variant-numeric: tabular-nums;
}

/* Row delete fade-out */
@keyframes row-fade-out {
  0%   { opacity: 1; max-height: 48px; }
  60%  { opacity: 0; max-height: 48px; }
  100% { opacity: 0; max-height: 0; padding-top: 0; padding-bottom: 0; border: none; }
}
.row-deleting td { overflow: hidden; animation: row-fade-out .5s ease forwards; }

/* Spinner / Progress overlay */
.spinner-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  z-index: 9000;
}
.spinner-overlay.hidden { display: none; }
.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(255,255,255,.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
.spinner-label {
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: .02em;
  text-align: center;
  line-height: 1.8;
}
.spinner-label div:nth-child(2) {
  font-size: 13px;
  font-weight: 400;
  color: rgba(255,255,255,.7);
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 999;
  animation: slide-in .3s ease;
  max-width: 340px;
}
.toast.success { background: var(--success); color: #fff; }
.toast.error   { background: var(--danger);  color: #fff; }
.toast.info    { background: var(--info);    color: #fff; }
.toast.hidden  { display: none; }

@keyframes slide-in {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* Preset bar */
.preset-bar { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.preset-label { font-size: 12px; color: var(--text-muted); }
.password-hint { font-size: 12px; font-weight: 400; margin-left: 6px; }
.password-hint a { color: var(--primary); text-decoration: none; }
.password-hint a:hover { text-decoration: underline; }

/* Login */
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: calc(100vh - 56px); }
.login-card { width: 100%; max-width: 380px; padding: 36px; }
.login-logo { font-size: 24px; font-weight: 700; color: var(--primary); text-align: center; margin-bottom: 8px; }
.login-error { color: var(--danger); font-size: 13px; margin-top: 12px; text-align: center; }
.login-error.hidden { display: none; }

/* fetch-actions */
.fetch-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* Misc */
.text-muted { color: var(--text-muted); font-size: 13px; margin-bottom: 16px; }
.status-text { color: var(--text-muted); font-size: 13px; }
.empty { color: var(--text-muted); padding: 24px 0; text-align: center; }

/* Confirm Dialog */
.confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 8000;
  padding: 16px;
}
.confirm-overlay.hidden { display: none; }
.confirm-dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px 20px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
  animation: dialog-in .18s ease;
}
@keyframes dialog-in {
  from { transform: scale(.95); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}
.confirm-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 12px;
}
.confirm-body {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.7;
  margin-bottom: 20px;
  word-break: break-all;
}
.confirm-body strong { color: var(--text); }
.confirm-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* Activity Log Panel */
.log-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 180px;
  background: #0a0c12;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 500;
  font-family: 'Menlo', 'Consolas', monospace;
  font-size: 12px;
}
.log-resize-handle {
  position: absolute;
  top: -4px;
  left: 0;
  right: 0;
  height: 8px;
  cursor: ns-resize;
  z-index: 10;
}
.log-resize-handle::before {
  content: '';
  display: block;
  position: absolute;
  top: 3px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 2px;
  background: var(--border);
  border-radius: 2px;
  transition: background .2s;
}
.log-resize-handle:hover::before { background: var(--text-muted); }
.log-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  cursor: pointer;
  user-select: none;
}
.log-panel-header span { color: var(--text-muted); font-size: 11px; letter-spacing: .04em; }
.log-clear { color: var(--text-muted); background: none; border: none; cursor: pointer; font-size: 11px; padding: 2px 6px; border-radius: 3px; }
.log-clear:hover { color: var(--text); background: var(--border); }
.log-body {
  overflow-y: auto;
  flex: 1;
  padding: 6px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.log-panel.collapsed { height: 28px; }
.log-panel.collapsed .log-body { display: none; }
.log-entry { display: flex; gap: 8px; line-height: 1.5; white-space: pre-wrap; word-break: break-all; }
.log-time { color: #4a5568; flex-shrink: 0; }
.log-msg { color: var(--text); }
.log-msg.info  { color: #94a3b8; }
.log-msg.ok    { color: var(--success); }
.log-msg.error { color: var(--danger); }
.log-msg.warn  { color: #f59e0b; }

/* DB取得済み期間バッジ */
.db-range-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  background: var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  margin-left: 8px;
  vertical-align: middle;
}

body { padding-bottom: 180px; }
body.log-collapsed { padding-bottom: 28px; }

/* Responsive */
@media (max-width: 600px) {
  .card { padding: 16px; }
  .hero h1 { font-size: 28px; }
  .hero { padding: 40px 0; }
  .hero-actions { flex-direction: column; align-items: stretch; }
  .ranking-toolbar { gap: 6px; }
  .ranking-toolbar .btn-danger { width: 100%; justify-content: center; margin-top: 4px; }
  .fetch-actions { flex-direction: column; align-items: flex-start; gap: 6px; }
  .status-text { margin-left: 0; }
  .login-card { padding: 24px 16px; }
  /* フォームを1列に */
  .form-row { flex-direction: column; gap: 8px; }
  label { flex: 1 1 auto; }
}
