/*
 * Affilio デザインシステム
 * 外部CDN・ビルドステップなし（共有サーバー前提のためプレーンCSSのみ）。
 * ライト/ダーク両対応。CSS変数でトークン化し、コンポーネント側は
 * 変数を参照するだけにする。
 */

:root {
  --color-bg: #f6f7fb;
  --color-surface: #ffffff;
  --color-surface-muted: #f1f2f8;
  --color-border: #e3e5ef;
  --color-text: #1a1c29;
  --color-text-muted: #666a80;
  --color-text-faint: #9497ab;

  --color-primary: #5b5bf0;
  --color-primary-hover: #4747d8;
  --color-primary-soft: #eeeeff;
  --color-danger: #e5484d;
  --color-danger-hover: #cf3d42;
  --color-danger-soft: #fdecec;
  --color-success: #1ea672;
  --color-success-soft: #e6f7f0;
  --color-warning: #b9740a;
  --color-warning-soft: #fdf1de;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(20, 21, 40, 0.04), 0 1px 1px rgba(20, 21, 40, 0.03);
  --shadow-md: 0 8px 24px rgba(20, 21, 40, 0.08);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", Roboto, sans-serif;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #121319;
    --color-surface: #1b1c26;
    --color-surface-muted: #23242f;
    --color-border: #2e303e;
    --color-text: #eef0fa;
    --color-text-muted: #a4a7bd;
    --color-text-faint: #71748c;

    --color-primary: #8484ff;
    --color-primary-hover: #9a9aff;
    --color-primary-soft: #23233f;
    --color-danger: #ff6b70;
    --color-danger-hover: #ff8388;
    --color-danger-soft: #3a2126;
    --color-success: #37cf98;
    --color-success-soft: #133528;
    --color-warning: #e0a740;
    --color-warning-soft: #3a2c11;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 12px 28px rgba(0, 0, 0, 0.45);

    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--color-primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

h1,
h2,
h3 {
  line-height: 1.3;
  font-weight: 700;
  letter-spacing: -0.01em;
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 4px;
}

h2 {
  font-size: 1.15rem;
  margin: 32px 0 12px;
}

h3 {
  font-size: 1rem;
  margin: 20px 0 8px;
}

p {
  color: var(--color-text-muted);
}

/* ----- レイアウト ----------------------------------------------------- */

.topnav {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.topnav__brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  font-size: 1.05rem;
  color: var(--color-text);
}

.topnav__brand::before {
  content: "";
  display: inline-block;
  width: 20px;
  height: 20px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--color-primary), #a78bfa);
}

.topnav__links {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.topnav__link {
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--color-text-muted);
}

.topnav__link:hover {
  background: var(--color-surface-muted);
  text-decoration: none;
  color: var(--color-text);
}

.topnav__link--active {
  background: var(--color-primary-soft);
  color: var(--color-primary);
}

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 32px 24px 64px;
}

.container--narrow {
  max-width: 480px;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}

.page-header__meta p {
  margin: 4px 0 0;
}

.breadcrumb {
  font-size: 0.85rem;
  color: var(--color-text-faint);
  margin-bottom: 4px;
}

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

/* ----- 認証・インストール用の中央カード画面 ------------------------------ */

.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 32px;
}

.auth-card__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  font-size: 1.3rem;
  margin-bottom: 4px;
}

.auth-card__brand::before {
  content: "";
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, var(--color-primary), #a78bfa);
}

/* ----- カード / パネル ------------------------------------------------- */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 24px;
  margin-bottom: 20px;
}

.card + .card {
  margin-top: 20px;
}

.card__title {
  margin: 0 0 16px;
  font-size: 1rem;
  font-weight: 700;
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--color-text-faint);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-md);
}

/* ----- ボタン ---------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.05s ease;
  text-decoration: none;
  line-height: 1.2;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--color-primary-hover);
  text-decoration: none;
  color: #fff;
}

.btn-secondary {
  background: var(--color-surface);
  border-color: var(--color-border);
  color: var(--color-text);
}

.btn-secondary:hover {
  background: var(--color-surface-muted);
  text-decoration: none;
  color: var(--color-text);
}

.btn-ghost {
  background: transparent;
  color: var(--color-text-muted);
  padding: 9px 12px;
}

.btn-ghost:hover {
  background: var(--color-surface-muted);
  text-decoration: none;
  color: var(--color-text);
}

.btn-danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

.btn-danger:hover {
  background: var(--color-danger);
  color: #fff;
  text-decoration: none;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.8rem;
}

.btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn.is-loading {
  opacity: 0.85;
  cursor: wait;
  position: relative;
  padding-left: 34px;
}

.btn.is-loading::before {
  content: "";
  position: absolute;
  left: 12px;
  top: 50%;
  width: 14px;
  height: 14px;
  margin-top: -7px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: btn-spin 0.6s linear infinite;
}

.btn-secondary.is-loading::before,
.btn-ghost.is-loading::before {
  border-color: rgba(0, 0, 0, 0.15);
  border-top-color: var(--color-text);
}

@keyframes btn-spin {
  to { transform: rotate(360deg); }
}

.inline-form {
  display: inline-block;
}

/* ----- フォーム --------------------------------------------------------- */

.field {
  margin-bottom: 18px;
}

.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--color-text);
}

.field__hint {
  display: block;
  font-size: 0.78rem;
  color: var(--color-text-faint);
  margin-top: 4px;
  font-weight: 400;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="url"],
input[type="number"],
input[type="date"],
input[type="month"],
input[type="datetime-local"],
textarea,
select {
  width: 100%;
  padding: 9px 12px;
  font-size: 0.92rem;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

select[multiple] {
  min-height: 120px;
}

textarea {
  resize: vertical;
  min-height: 90px;
  font-family: inherit;
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}

.form-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.form-row > .field {
  flex: 1 1 200px;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.checkbox-field input {
  width: auto;
}

/* ----- テーブル --------------------------------------------------------- */

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
  font-size: 0.9rem;
}

thead th {
  text-align: left;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-faint);
  font-weight: 700;
  padding: 12px 16px;
  background: var(--color-surface-muted);
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
  white-space: nowrap;
}

tbody td.wrap {
  white-space: normal;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background: var(--color-surface-muted);
}

.cell-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* ----- バッジ / ステータス ------------------------------------------------ */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--color-surface-muted);
  color: var(--color-text-muted);
  white-space: nowrap;
}

.badge-neutral {
  background: var(--color-surface-muted);
  color: var(--color-text-muted);
}

.badge-primary {
  background: var(--color-primary-soft);
  color: var(--color-primary);
}

.badge-success {
  background: var(--color-success-soft);
  color: var(--color-success);
}

.badge-warning {
  background: var(--color-warning-soft);
  color: var(--color-warning);
}

.badge-danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

/* ----- アラート --------------------------------------------------------- */

.alert {
  border-radius: var(--radius-md);
  padding: 12px 16px;
  margin-bottom: 20px;
  font-size: 0.88rem;
}

.alert-error {
  background: var(--color-danger-soft);
  color: var(--color-danger);
  border: 1px solid transparent;
}

.alert-error ul {
  margin: 4px 0 0;
  padding-left: 18px;
}

.alert-info {
  background: var(--color-primary-soft);
  color: var(--color-primary);
}

.alert-success {
  background: var(--color-success-soft);
  color: var(--color-success);
}

.flash {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-radius: var(--radius-md);
  padding: 12px 16px;
  margin-bottom: 20px;
  font-size: 0.88rem;
  font-weight: 600;
  animation: flash-in 0.15s ease-out;
}

.flash button {
  background: none;
  border: none;
  cursor: pointer;
  color: inherit;
  opacity: 0.6;
  font-size: 1rem;
  line-height: 1;
  padding: 0;
}

.flash button:hover {
  opacity: 1;
}

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

/* ----- 画像プレビュー ------------------------------------------------- */

.thumb-preview {
  display: inline-block;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--color-border);
  background: var(--color-surface-muted);
}

.thumb-preview img {
  display: block;
  width: 160px;
  height: auto;
}

.thumb-grid {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin: 8px 0 16px;
}

/* ----- Markdownプレビュー ------------------------------------------------ */

.markdown-preview {
  margin-top: 8px;
  padding: 16px 20px;
  min-height: 400px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

.markdown-preview h1,
.markdown-preview h2,
.markdown-preview h3,
.markdown-preview h4,
.markdown-preview h5,
.markdown-preview h6 {
  margin: 20px 0 8px;
}

.markdown-preview h1:first-child,
.markdown-preview h2:first-child,
.markdown-preview h3:first-child {
  margin-top: 0;
}

.markdown-preview p {
  color: var(--color-text);
  margin: 0 0 14px;
  line-height: 1.8;
}

/* ----- ユーティリティ ---------------------------------------------------- */

.text-muted {
  color: var(--color-text-muted);
}

.text-faint {
  color: var(--color-text-faint);
}

.stat {
  font-size: 1.4rem;
  font-weight: 800;
}

.divider {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 24px 0;
}

.app-footer {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding: 24px;
  font-size: 0.8rem;
}

.app-footer a {
  color: var(--color-text-faint);
}

@media (max-width: 640px) {
  .topnav {
    flex-direction: column;
    align-items: flex-start;
  }

  .container {
    padding: 20px 16px 48px;
  }

  .cell-actions {
    justify-content: flex-start;
  }
}
