/* Caloud Design System — 通用元件
 * 規格：docs/05-components.md §A。只能引用 tokens.css 與 theme.css 定義的 Token。
 * 命名：c- 元件、c-x__y 子元素、c-x--y 變體、l- 版面、u- 工具、is- 狀態。 */

/* ───────── 基礎 ───────── */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: var(--text-md);
  line-height: var(--leading);
  background: var(--color-bg);
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}
a { color: var(--color-accent); text-underline-offset: 2px; }
a:hover { color: var(--color-accent-hover); }
:focus-visible { outline: var(--focus-ring); outline-offset: 2px; }
h1, h2, h3 { margin: 0; font-family: var(--font-display); line-height: var(--leading-tight); text-wrap: balance; }
p { margin: 0; }
.c-icon { width: 16px; height: 16px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }

/* ───────── 版面 ───────── */
.l-stack { display: flex; flex-direction: column; gap: var(--space-4); min-width: 0; }
.l-row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); min-width: 0; }
.l-grid { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.l-spacer { flex: 1; }

/* ───────── AppShell ───────── */
.c-shell { display: grid; grid-template-columns: var(--sidebar-width) minmax(0, 1fr); min-height: 100vh; }
.c-shell__main { min-width: 0; }

.c-sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: var(--space-4);
  padding: var(--space-4) var(--space-3);
  background: var(--color-sidebar-bg); color: var(--color-sidebar-text);
  border-right: 1px solid var(--color-sidebar-border);
  min-width: 0;
}
.c-sidebar__brand { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-1) var(--space-2); color: var(--color-sidebar-text-active); font-family: var(--font-display); font-weight: var(--weight-semibold); font-size: var(--text-lg); }
.c-sidebar__mark { width: 24px; height: 24px; flex: none; display: block; } /* 品牌標誌圖（docs/04 §4.1） */
.c-sidebar__nav { display: flex; flex-direction: column; gap: 2px; }
.c-sidebar__item {
  display: flex; align-items: center; gap: var(--space-3);
  min-height: var(--control-height); padding: 0 var(--space-3);
  border: 0; border-radius: var(--radius-control); background: none;
  color: inherit; font: inherit; text-align: left; text-decoration: none; cursor: pointer;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.c-sidebar__item:hover { background: var(--color-sidebar-active-bg); color: var(--color-sidebar-text-active); }
.c-sidebar__item.is-active { background: var(--color-sidebar-active-bg); color: var(--color-sidebar-text-active); font-weight: var(--weight-medium); }
.c-sidebar__count { margin-left: auto; font-size: var(--text-xs); padding: 0 var(--space-2); border-radius: 999px; background: var(--color-warning); color: var(--color-sidebar-bg); font-weight: var(--weight-semibold); }
.c-sidebar__footer { margin-top: auto; display: flex; flex-direction: column; gap: var(--space-2); padding-top: var(--space-3); border-top: 1px solid var(--color-sidebar-border); }

@media (max-width: 768px) {
  .c-shell { grid-template-columns: minmax(0, 1fr); }
  .c-sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: var(--space-2) var(--space-4); border-right: 0; border-bottom: 1px solid var(--color-sidebar-border); }
  .c-sidebar__nav { flex-direction: row; overflow-x: auto; flex: 1 1 100%; min-width: 0; order: 3; }
  .c-sidebar__item { white-space: nowrap; }
  .c-sidebar__footer { margin: 0 0 0 auto; flex-direction: row; align-items: center; padding: 0; border: 0; }
}

/* ───────── Page ───────── */
.c-page { max-width: var(--content-width); margin: 0 auto; padding: var(--space-8) max(var(--space-4), env(safe-area-inset-right, 0px)) var(--space-16) max(var(--space-4), env(safe-area-inset-left, 0px)); display: flex; flex-direction: column; gap: var(--space-6); }
.c-page-header { display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--space-4); }
.c-page-header__text { flex: 1 1 320px; min-width: 0; display: flex; flex-direction: column; gap: var(--space-1); }
.c-page-header__title { font-size: var(--text-2xl); font-weight: var(--weight-semibold); }
.c-page-header__desc { color: var(--color-text-muted); max-width: 70ch; }
.c-page-header__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }

.c-section-title { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.c-label { font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--color-text-muted); }

/* ───────── Button ───────── */
.c-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--control-height); padding: 0 var(--space-3);
  font: inherit; font-weight: var(--weight-medium); line-height: 1; white-space: nowrap;
  color: var(--color-text); background: var(--color-surface);
  border: var(--border-strong); border-radius: var(--radius-control);
  cursor: pointer; text-decoration: none;
  transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease), color var(--duration) var(--ease);
}
.c-btn:hover { background: var(--color-surface-hover); color: var(--color-text); }
.c-btn:disabled, .c-btn.is-loading { opacity: 0.55; cursor: not-allowed; }
.c-btn.is-loading .c-icon { animation: kf-spin 0.8s linear infinite; }
@keyframes kf-spin { to { transform: rotate(360deg); } }
.c-btn--primary { background: var(--color-accent); color: var(--color-on-accent); border-color: transparent; }
.c-btn--primary:hover { background: var(--color-accent-hover); color: var(--color-on-accent); }
.c-btn--ghost { background: transparent; border-color: transparent; color: var(--color-text-muted); }
.c-btn--ghost:hover { background: var(--color-surface-hover); color: var(--color-text); }
.c-btn--danger { color: var(--color-danger); }
.c-btn--danger:hover { background: var(--color-danger-subtle); color: var(--color-danger); }
.c-btn--danger.c-btn--primary { background: var(--color-danger); color: var(--color-surface); }
.c-btn--sm { min-height: 28px; padding: 0 var(--space-2); font-size: var(--text-sm); }
.c-btn--icon { width: var(--control-height); padding: 0; }
.c-btn--icon.c-btn--sm { width: 28px; }
.c-sidebar .c-btn--ghost { color: var(--color-sidebar-text); }
.c-sidebar .c-btn--ghost:hover { background: var(--color-sidebar-active-bg); color: var(--color-sidebar-text-active); }

/* ───────── Form ───────── */
.c-field { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.c-field__label { font-size: var(--text-sm); font-weight: var(--weight-medium); color: var(--color-text); }
.c-field__hint { font-size: var(--text-xs); color: var(--color-text-muted); }
.c-field__error { font-size: var(--text-xs); color: var(--color-danger); }
.c-field__error:empty { display: none; }
.c-input, .c-select, .c-textarea {
  min-height: var(--control-height); width: 100%;
  padding: 0 var(--space-3);
  font: inherit; color: var(--color-text);
  background: var(--color-surface); border: var(--border-strong); border-radius: var(--radius-control);
  transition: border-color var(--duration) var(--ease);
}
.c-input:hover, .c-select:hover, .c-textarea:hover { border-color: var(--color-text-subtle); }
.c-input[aria-invalid="true"] { border-color: var(--color-danger); }
.c-input::placeholder, .c-textarea::placeholder { color: var(--color-text-subtle); }
.c-select { padding-right: var(--space-6); }
.c-textarea { min-height: 240px; padding: var(--space-3); font-family: var(--font-mono); font-size: var(--text-sm); line-height: var(--leading); border-radius: var(--radius-card); resize: vertical; }
.c-textarea--sm { min-height: 96px; }
.c-check { display: inline-flex; align-items: flex-start; gap: var(--space-2); cursor: pointer; font-size: var(--text-sm); }
.c-check input { width: 16px; height: 16px; margin: 2px 0 0; flex: none; accent-color: var(--color-accent); }
.c-search { position: relative; flex: 1 1 240px; max-width: 360px; }
.c-search .c-icon { position: absolute; left: var(--space-3); top: 50%; transform: translateY(-50%); color: var(--color-text-subtle); pointer-events: none; }
.c-search .c-input { padding-left: calc(var(--space-3) * 2 + 16px); }

/* ───────── Segmented / Tabs ───────── */
.c-segmented { display: inline-flex; padding: 2px; gap: 2px; background: var(--color-surface-sunken); border: var(--border); border-radius: var(--radius-control); }
.c-segmented__item { min-height: calc(var(--control-height) - 6px); padding: 0 var(--space-3); border: 0; border-radius: var(--radius-control); background: none; font: inherit; font-size: var(--text-sm); color: var(--color-text-muted); cursor: pointer; }
.c-segmented__item.is-active { background: var(--color-surface); color: var(--color-text); font-weight: var(--weight-medium); box-shadow: var(--shadow-card); }
.c-tabs { display: flex; gap: var(--space-4); border-bottom: var(--border); overflow-x: auto; }
.c-tabs__item { padding: var(--space-2) 0; border: 0; border-bottom: 2px solid transparent; margin-bottom: -1px; background: none; font: inherit; color: var(--color-text-muted); cursor: pointer; white-space: nowrap; }
.c-tabs__item.is-active { color: var(--color-text); border-bottom-color: var(--color-accent); font-weight: var(--weight-medium); }

/* ───────── Card / Stat ───────── */
.c-card { background: var(--color-surface); border: var(--border); border-radius: var(--radius-card); box-shadow: var(--shadow-card); min-width: 0; }
.c-card__header { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-bottom: var(--border); }
.c-card__title { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.c-card__body { padding: var(--space-5); display: flex; flex-direction: column; gap: var(--space-4); }
.c-card__body--flush { padding: 0; }

.c-stat { background: var(--color-surface); border: var(--border); border-radius: var(--radius-card); box-shadow: var(--shadow-card); padding: var(--space-4) var(--space-5); display: flex; flex-direction: column; gap: var(--space-1); text-align: left; font: inherit; color: inherit; }
a.c-stat, button.c-stat { cursor: pointer; text-decoration: none; transition: border-color var(--duration) var(--ease); }
a.c-stat:hover, button.c-stat:hover { border-color: var(--color-border-strong); color: inherit; }
.c-stat__label { font-size: var(--text-sm); color: var(--color-text-muted); }
.c-stat__value { font-family: var(--font-display); font-size: var(--text-3xl); font-weight: var(--weight-semibold); line-height: var(--leading-tight); }
.c-stat__meta { font-size: var(--text-sm); color: var(--color-text-muted); display: flex; align-items: center; gap: var(--space-2); }

/* ───────── Table ───────── */
.c-table-wrap { position: relative; overflow-x: auto; min-width: 0; }
.c-table { width: 100%; border-collapse: collapse; }
.c-table th, .c-table td { padding: var(--space-3) var(--space-4); text-align: left; vertical-align: middle; border-bottom: var(--border); }
.c-table tr:last-child td { border-bottom: 0; }
.c-table th { font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--color-text-muted); background: var(--color-surface-sunken); white-space: nowrap; }
.c-table tr.is-clickable { cursor: pointer; }
.c-table tr.is-clickable:hover td { background: var(--color-surface-hover); }
.c-table__primary { font-weight: var(--weight-medium); color: var(--color-text); }
.c-table__secondary { font-size: var(--text-xs); color: var(--color-text-muted); }
.c-table td .u-truncate { max-width: 36ch; }
.c-table__actions { display: flex; justify-content: flex-end; gap: var(--space-1); }

/* ───────── Badge（StatusPill） ───────── */
.c-badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px var(--space-2); border-radius: 999px; font-size: var(--text-xs); font-weight: var(--weight-medium); line-height: 18px; white-space: nowrap; color: var(--color-text-muted); background: var(--color-surface-sunken); }
.c-badge__dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.c-badge--success { color: var(--color-success); background: var(--color-success-subtle); }
.c-badge--warning { color: var(--color-warning); background: var(--color-warning-subtle); }
.c-badge--danger { color: var(--color-danger); background: var(--color-danger-subtle); }
.c-badge--accent { color: var(--color-accent); background: var(--color-accent-subtle); }
.c-badge--pulse .c-badge__dot { animation: kf-pulse 1.4s var(--ease) infinite; }
@keyframes kf-pulse { 50% { opacity: 0.3; } }
@media (prefers-reduced-motion: reduce) { .c-badge--pulse .c-badge__dot { animation: none; } }

/* ───────── Banner / Msg ───────── */
.c-banner { display: flex; align-items: flex-start; gap: var(--space-3); padding: var(--space-3) var(--space-4); border-radius: var(--radius-card); background: var(--color-accent-subtle); color: var(--color-text); border: 1px solid transparent; }
.c-banner > .c-icon { margin-top: 2px; color: var(--color-accent); }
.c-banner__body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: var(--space-1); }
.c-banner--warning { background: var(--color-warning-subtle); }
.c-banner--warning > .c-icon { color: var(--color-warning); }
.c-banner--danger { background: var(--color-danger-subtle); }
.c-banner--danger > .c-icon { color: var(--color-danger); }
.c-msg { font-size: var(--text-sm); }
.c-msg:empty { display: none; }
.c-msg--danger { color: var(--color-danger); }

/* ───────── Empty / Skeleton ───────── */
.c-empty { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); padding: var(--space-10) var(--space-4); text-align: center; color: var(--color-text-muted); }
.c-empty > .c-icon { width: 28px; height: 28px; color: var(--color-text-subtle); }
.c-empty__title { color: var(--color-text); font-weight: var(--weight-semibold); }
.c-skeleton { display: block; height: 14px; border-radius: 4px; background: linear-gradient(90deg, var(--color-surface-sunken), var(--color-surface-hover), var(--color-surface-sunken)); background-size: 200% 100%; animation: kf-shimmer 1.2s linear infinite; }
@keyframes kf-shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .c-skeleton { animation: none; } }

/* ───────── Code / Log / Copy / KV ───────── */
.c-code { font-family: var(--font-mono); font-size: 0.92em; padding: 1px var(--space-1); border-radius: 4px; background: var(--color-surface-sunken); word-break: break-all; }
.c-log { margin: 0; padding: var(--space-3) var(--space-4); max-height: 420px; overflow: auto; font-family: var(--font-mono); font-size: var(--text-xs); line-height: 1.6; background: var(--color-surface-sunken); border: var(--border); border-radius: var(--radius-card); white-space: pre-wrap; word-break: break-all; }
.c-copy { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-1) var(--space-1) var(--space-1) var(--space-3); background: var(--color-surface-sunken); border: var(--border); border-radius: var(--radius-control); min-width: 0; }
.c-copy__value { flex: 1; min-width: 0; font-family: var(--font-mono); font-size: var(--text-sm); overflow-x: auto; white-space: nowrap; }
.c-kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--space-2) var(--space-6); margin: 0; }
.c-kv__key { color: var(--color-text-muted); font-size: var(--text-sm); }
.c-kv__value { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* ───────── Stepper ───────── */
.c-stepper { display: flex; flex-direction: column; gap: 0; margin: 0; padding: 0; list-style: none; }
.c-stepper__step { position: relative; display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: var(--space-3); padding-bottom: var(--space-6); }
.c-stepper__step:not(:last-child)::before { content: ""; position: absolute; left: 13px; top: 30px; bottom: 2px; width: 2px; background: var(--color-border); }
.c-stepper__step.is-done:not(:last-child)::before { background: var(--color-success); }
.c-stepper__index { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-size: var(--text-sm); font-weight: var(--weight-semibold); border: 2px solid var(--color-border-strong); color: var(--color-text-muted); background: var(--color-surface); }
.c-stepper__step.is-current .c-stepper__index { border-color: var(--color-accent); color: var(--color-accent); }
.c-stepper__step.is-done .c-stepper__index { border-color: var(--color-success); background: var(--color-success); color: var(--color-surface); }
.c-stepper__content { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; padding-top: 3px; }
.c-stepper__label { font-weight: var(--weight-semibold); }
.c-stepper__step:not(.is-current):not(.is-done) .c-stepper__label { color: var(--color-text-muted); }

/* ───────── Drawer / Dialog ───────── */
.c-drawer, .c-dialog { padding: 0; border: var(--border); color: var(--color-text); background: var(--color-surface-raised); box-shadow: var(--shadow-overlay); }
.c-drawer::backdrop, .c-dialog::backdrop { background: var(--color-overlay); }
.c-drawer { margin: 0 0 0 auto; width: min(var(--drawer-width), 100vw); max-width: 100vw; height: 100dvh; max-height: 100dvh; border-radius: var(--radius-overlay) 0 0 var(--radius-overlay); }
.c-drawer[open] { display: flex; flex-direction: column; animation: kf-slide-in var(--duration-slow) var(--ease); }
@keyframes kf-slide-in { from { transform: translateX(24px); opacity: 0; } }
.c-drawer__header { display: flex; align-items: flex-start; gap: var(--space-3); padding: var(--space-5) var(--space-6) var(--space-4); border-bottom: var(--border); }
.c-drawer__heading { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: var(--space-1); }
.c-drawer__title { font-size: var(--text-xl); font-weight: var(--weight-semibold); }
.c-drawer__body { flex: 1; overflow-y: auto; padding: var(--space-5) var(--space-6); display: flex; flex-direction: column; gap: var(--space-5); }
.c-drawer__footer { display: flex; justify-content: flex-end; gap: var(--space-2); padding: var(--space-4) var(--space-6); border-top: var(--border); }
.c-dialog { width: min(440px, calc(100vw - 32px)); border-radius: var(--radius-overlay); }
.c-dialog__body { padding: var(--space-6); display: flex; flex-direction: column; gap: var(--space-2); }
.c-dialog__title { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.c-dialog__actions { display: flex; justify-content: flex-end; gap: var(--space-2); padding: var(--space-4) var(--space-6); background: var(--color-surface-sunken); border-top: var(--border); }

/* ───────── Toast ───────── */
.c-toast-region { position: fixed; z-index: var(--z-toast); right: max(var(--space-4), env(safe-area-inset-right, 0px)); bottom: max(var(--space-4), env(safe-area-inset-bottom, 0px)); display: flex; flex-direction: column; gap: var(--space-2); pointer-events: none; }
.c-toast { pointer-events: auto; display: flex; align-items: center; gap: var(--space-3); min-width: 240px; max-width: min(420px, calc(100vw - 32px)); padding: var(--space-3) var(--space-4); background: var(--color-surface-raised); color: var(--color-text); border: var(--border); border-radius: var(--radius-card); box-shadow: var(--shadow-overlay); animation: kf-toast-in var(--duration-slow) var(--ease); }
@keyframes kf-toast-in { from { transform: translateY(8px); opacity: 0; } }
.c-toast--success > .c-icon { color: var(--color-success); }
.c-toast--danger > .c-icon { color: var(--color-danger); }

/* ───────── Account ───────── */
.c-account { display: flex; align-items: center; gap: var(--space-2); min-width: 0; padding: var(--space-1) var(--space-2); }
.c-account__avatar { width: 28px; height: 28px; flex: none; border-radius: 50%; background: var(--brand-mark); }
.c-account__text { display: flex; flex-direction: column; min-width: 0; line-height: var(--leading-tight); }
.c-account__name { color: var(--color-sidebar-text-active); font-size: var(--text-sm); font-weight: var(--weight-medium); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-account__role { font-size: var(--text-xs); }

/* ───────── Login ───────── */
.c-login { min-height: 100vh; display: grid; place-items: center; padding: var(--space-6) max(var(--space-4), env(safe-area-inset-right, 0px)) var(--space-6) max(var(--space-4), env(safe-area-inset-left, 0px)); }
.c-login__panel { width: min(400px, 100%); display: flex; flex-direction: column; align-items: center; gap: var(--space-4); padding: var(--space-10) var(--space-8); text-align: center; background: var(--color-surface); border: var(--border); border-radius: var(--radius-overlay); box-shadow: var(--shadow-card); }
.c-login__mark { width: 48px; height: 48px; display: block; } /* 品牌標誌圖（docs/04 §4.1） */
.c-login__title { font-size: var(--text-2xl); font-weight: var(--weight-semibold); }
.c-login__desc { color: var(--color-text-muted); }
.c-login .c-btn { width: 100%; min-height: 44px; }

/* ───────── 工具 ───────── */
.u-muted { color: var(--color-text-muted); }
.u-small { font-size: var(--text-sm); }
.u-mono { font-family: var(--font-mono); }
.u-nowrap { white-space: nowrap; }
.u-truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-hidden { display: none !important; }
.u-sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
