/* =========================================================================
   MacroKey — Component Library
   Reusable UI primitives shared by the website (apps/web) and the desktop
   app (app/). Depends on theme/tokens.css + theme/base.css.
   ========================================================================= */

/* ============================ Buttons ================================== */
.btn {
  --btn-bg: var(--surface-2);
  --btn-fg: var(--text);
  --btn-bd: var(--border-strong);
  display: inline-flex; align-items: center; justify-content: center; gap: .55em;
  font-family: var(--font-sans); font-size: var(--fs-sm); font-weight: var(--fw-semibold);
  line-height: 1; letter-spacing: -0.005em; white-space: nowrap;
  padding: .72em 1.15em; border-radius: var(--r-md);
  background: var(--btn-bg); color: var(--btn-fg);
  border: 1px solid var(--btn-bd);
  transition: transform var(--dur-1) var(--ease), background var(--dur-2) var(--ease),
              border-color var(--dur-2) var(--ease), box-shadow var(--dur-2) var(--ease), color var(--dur-2) var(--ease);
  user-select: none;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.btn svg { width: 1.15em; height: 1.15em; flex: none; }

.btn-primary {
  --btn-bg: var(--primary); --btn-fg: var(--on-primary); --btn-bd: transparent;
  background: var(--grad-brand); box-shadow: var(--shadow-sm);
}
.btn-primary:hover { box-shadow: var(--shadow-glow); }
.btn-secondary { --btn-bg: var(--surface); --btn-fg: var(--text); --btn-bd: var(--border-strong); }
.btn-secondary:hover { --btn-bd: var(--primary); background: var(--primary-softer); }
.btn-ghost { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--text-muted); }
.btn-ghost:hover { --btn-fg: var(--text); background: var(--surface-2); transform: none; }
.btn-danger { --btn-bg: var(--danger-soft); --btn-fg: var(--danger); --btn-bd: transparent; }
.btn-danger:hover { --btn-bg: var(--danger); --btn-fg: #fff; }
.btn-success { --btn-bg: var(--success); --btn-fg: #04140d; --btn-bd: transparent; }

.btn-lg { font-size: var(--fs-md); padding: .95em 1.5em; border-radius: var(--r-lg); }
.btn-sm { font-size: var(--fs-xs); padding: .5em .8em; border-radius: var(--r-sm); }
.btn-icon { padding: .55em; aspect-ratio: 1; border-radius: var(--r-md); }
.btn-icon.btn-sm { padding: .4em; }
.btn-block { width: 100%; }
.btn-pill { border-radius: var(--r-full); }

/* Icon button used in dense app toolbars */
.iconbtn {
  display: inline-grid; place-items: center; width: 34px; height: 34px;
  border-radius: var(--r-sm); color: var(--text-muted); background: transparent;
  border: 1px solid transparent; transition: all var(--dur-2) var(--ease);
}
.iconbtn:hover { color: var(--text); background: var(--surface-2); border-color: var(--border); }
.iconbtn.is-active { color: var(--primary); background: var(--primary-soft); border-color: transparent; }
.iconbtn svg { width: 18px; height: 18px; }

/* ============================ Keycap =================================== */
/* The signature MacroKey element: a physical-looking key. */
.kbd, .keycap {
  display: inline-flex; align-items: center; justify-content: center; gap: .3em;
  min-width: 1.9em; height: 1.9em; padding: 0 .55em;
  font-family: var(--font-mono); font-size: .82em; font-weight: var(--fw-semibold);
  line-height: 1; color: var(--text);
  background: var(--keycap-bg); border: 1px solid var(--keycap-border);
  border-radius: var(--r-sm); box-shadow: var(--shadow-key);
  white-space: nowrap; vertical-align: middle;
}
.keycap-lg { font-size: 1rem; min-width: 2.6em; height: 2.6em; border-radius: var(--r-md); }
.keycap-accent { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 45%, var(--keycap-border)); }
.combo { display: inline-flex; align-items: center; gap: .3em; }
.combo .plus { color: var(--text-faint); font-size: .8em; font-weight: var(--fw-bold); }

/* ============================ Cards =================================== */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); padding: var(--sp-6);
  transition: border-color var(--dur-2) var(--ease), box-shadow var(--dur-2) var(--ease), transform var(--dur-2) var(--ease);
}
.card-hover:hover { border-color: var(--border-strong); box-shadow: var(--shadow-md); transform: translateY(-3px); }
.card-inset { background: var(--surface-2); border-color: var(--border-faint); }
.card-glass {
  background: color-mix(in srgb, var(--surface) 78%, transparent);
  backdrop-filter: blur(14px) saturate(1.3); -webkit-backdrop-filter: blur(14px) saturate(1.3);
}
.card-feature { padding: var(--sp-7); border-radius: var(--r-xl); }

.feature-icon {
  display: inline-grid; place-items: center; width: 48px; height: 48px;
  border-radius: var(--r-md); background: var(--primary-soft); color: var(--primary);
  border: 1px solid color-mix(in srgb, var(--primary) 24%, transparent);
}
.feature-icon svg { width: 24px; height: 24px; }
.feature-icon.tone-cyan { background: var(--info-soft); color: var(--info); border-color: color-mix(in srgb, var(--info) 24%, transparent); }
.feature-icon.tone-green { background: var(--success-soft); color: var(--success); border-color: color-mix(in srgb, var(--success) 24%, transparent); }
.feature-icon.tone-pink { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 24%, transparent); }
.feature-icon.tone-amber { background: var(--warning-soft); color: var(--warning); border-color: color-mix(in srgb, var(--warning) 24%, transparent); }

/* ============================ Badges / Pills ========================== */
.badge {
  display: inline-flex; align-items: center; gap: .4em;
  font-size: var(--fs-2xs); font-weight: var(--fw-semibold); letter-spacing: .01em;
  padding: .3em .65em; border-radius: var(--r-full);
  background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border);
}
.badge svg { width: 1em; height: 1em; }
.badge-primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.badge-success { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge-danger  { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-warning { background: var(--warning-soft); color: var(--warning); border-color: transparent; }
.badge-dot::before { content: ""; width: .5em; height: .5em; border-radius: 50%; background: currentColor; }

.pill-group { display: inline-flex; gap: var(--sp-2); flex-wrap: wrap; }

/* Live status dot with pulse */
.status-dot { position: relative; width: 9px; height: 9px; border-radius: 50%; background: var(--text-faint); flex: none; }
.status-dot.on { background: var(--success); }
.status-dot.rec { background: var(--danger); }
.status-dot.on::after, .status-dot.rec::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%;
  border: 2px solid currentColor; color: inherit; opacity: .5;
  animation: mk-ping 1.6s var(--ease-out) infinite;
}
.status-dot.on::after { color: var(--success); }
.status-dot.rec::after { color: var(--danger); }
@keyframes mk-ping { 0% { transform: scale(.7); opacity: .6; } 100% { transform: scale(1.8); opacity: 0; } }

/* ============================ Forms =================================== */
.field { display: flex; flex-direction: column; gap: var(--sp-2); }
.label { font-size: var(--fs-sm); font-weight: var(--fw-semibold); color: var(--text); }
.hint { font-size: var(--fs-xs); color: var(--text-faint); }

.input, .select, .textarea {
  width: 100%; font-size: var(--fs-sm); color: var(--text);
  background: var(--surface-inset); border: 1px solid var(--border-strong);
  border-radius: var(--r-sm); padding: .62em .8em;
  transition: border-color var(--dur-2) var(--ease), box-shadow var(--dur-2) var(--ease), background var(--dur-2) var(--ease);
}
.input::placeholder, .textarea::placeholder { color: var(--text-faint); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--ring); background: var(--surface);
}
.textarea { min-height: 90px; resize: vertical; line-height: var(--lh-snug); }
.input-mono { font-family: var(--font-mono); }
.select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b7186' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right .7em center; padding-right: 2.2em;
}
/* Chevron follows the theme (data-URIs can't read CSS variables). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238992a8' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  }
}
:root[data-theme="dark"] .select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238992a8' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
}
.input-group { display: flex; align-items: stretch; gap: 0; }
.input-group .input { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-group .btn { border-top-left-radius: 0; border-bottom-left-radius: 0; border-left: none; }

/* Toggle switch */
.switch { position: relative; display: inline-flex; align-items: center; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .track {
  width: 40px; height: 23px; border-radius: var(--r-full);
  background: var(--surface-3); border: 1px solid var(--border-strong);
  transition: background var(--dur-2) var(--ease), border-color var(--dur-2) var(--ease); position: relative;
}
.switch .thumb {
  position: absolute; top: 50%; left: 2px; transform: translateY(-50%);
  width: 17px; height: 17px; border-radius: 50%; background: #fff; box-shadow: var(--shadow-sm);
  transition: left var(--dur-2) var(--ease-spring);
}
.switch input:checked + .track { background: var(--primary); border-color: transparent; }
.switch input:checked + .track .thumb { left: 19px; }
.switch input:focus-visible + .track { box-shadow: 0 0 0 3px var(--ring); }

/* Segmented / Tabs */
.segmented { display: inline-flex; gap: 2px; padding: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-md); }
.segmented button {
  font-size: var(--fs-xs); font-weight: var(--fw-semibold); color: var(--text-muted);
  padding: .5em .9em; border-radius: var(--r-sm); transition: all var(--dur-2) var(--ease);
}
.segmented button:hover { color: var(--text); }
.segmented button.is-active { color: var(--text); background: var(--surface); box-shadow: var(--shadow-xs); }

.tabs { display: flex; gap: var(--sp-1); border-bottom: 1px solid var(--border); }
.tab {
  position: relative; padding: .75em .3em; margin-right: var(--sp-4);
  font-size: var(--fs-sm); font-weight: var(--fw-semibold); color: var(--text-muted);
  transition: color var(--dur-2) var(--ease);
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); }
.tab.is-active::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: var(--grad-brand); border-radius: 2px; }

/* Checkbox / radio mini */
.check { display: inline-flex; align-items: center; gap: .55em; font-size: var(--fs-sm); cursor: pointer; }
.check input { accent-color: var(--primary); width: 16px; height: 16px; }

/* ============================ Tables ================================== */
.table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.table th { text-align: left; font-weight: var(--fw-semibold); color: var(--text-faint); font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .06em; padding: .7em 1em; border-bottom: 1px solid var(--border); }
.table td { padding: .85em 1em; border-bottom: 1px solid var(--border-faint); }
.table tr:last-child td { border-bottom: none; }
.table-hover tbody tr { transition: background var(--dur-1) var(--ease); }
.table-hover tbody tr:hover { background: var(--surface-2); }

/* ============================ Tooltip ================================= */
.tip { position: relative; }
.tip::after {
  content: attr(data-tip); position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%) translateY(4px);
  background: var(--text); color: var(--bg-elevated); font-size: var(--fs-2xs); font-weight: var(--fw-semibold);
  padding: .4em .6em; border-radius: var(--r-sm); white-space: nowrap; pointer-events: none;
  opacity: 0; transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease); z-index: var(--z-tooltip);
}
.tip:hover::after { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============================ Dividers / misc ========================= */
.divider { height: 1px; background: var(--border); border: none; }
.divider-fade { height: 1px; border: none; background: linear-gradient(90deg, transparent, var(--border-strong), transparent); }
.dot-sep { color: var(--text-faint); }

/* ============================ Code ==================================== */
.code {
  font-family: var(--font-mono); font-size: var(--fs-sm); line-height: 1.7;
  background: var(--surface-inset); border: 1px solid var(--border);
  border-radius: var(--r-md); padding: var(--sp-4) var(--sp-5); overflow-x: auto; color: var(--text);
}
.code .tok-key { color: var(--primary); }
.code .tok-str { color: var(--success); }
.code .tok-com { color: var(--text-faint); font-style: italic; }
.inline-code { font-family: var(--font-mono); font-size: .9em; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-xs); padding: .1em .4em; }

/* ============================ Modal / Toast =========================== */
.modal-backdrop {
  position: fixed; inset: 0; z-index: var(--z-modal);
  background: color-mix(in srgb, #05060a 62%, transparent); backdrop-filter: blur(4px);
  display: grid; place-items: center; padding: var(--sp-5);
  opacity: 0; pointer-events: none; transition: opacity var(--dur-2) var(--ease);
}
.modal-backdrop.open { opacity: 1; pointer-events: auto; }
.modal {
  width: min(560px, 100%); background: var(--bg-elevated); border: 1px solid var(--border-strong);
  border-radius: var(--r-xl); box-shadow: var(--shadow-lg); padding: var(--sp-7);
  transform: translateY(10px) scale(.98); transition: transform var(--dur-3) var(--ease-spring);
  max-height: 88vh; overflow-y: auto;
}
.modal-backdrop.open .modal { transform: none; }

.toast-wrap { position: fixed; bottom: var(--sp-6); right: var(--sp-6); z-index: var(--z-toast); display: flex; flex-direction: column; gap: var(--sp-2); }
.toast {
  display: flex; align-items: center; gap: var(--sp-3); min-width: 240px; max-width: 380px;
  background: var(--bg-elevated); border: 1px solid var(--border-strong); border-left: 3px solid var(--primary);
  border-radius: var(--r-md); box-shadow: var(--shadow-lg); padding: var(--sp-3) var(--sp-4); font-size: var(--fs-sm);
  animation: mk-toast-in var(--dur-3) var(--ease-spring);
}
.toast.ok { border-left-color: var(--success); }
.toast.err { border-left-color: var(--danger); }
.toast svg { width: 18px; height: 18px; flex: none; }
@keyframes mk-toast-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }

/* ============================ Progress =============================== */
.progress { height: 6px; background: var(--surface-3); border-radius: var(--r-full); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--grad-brand); border-radius: inherit; transition: width var(--dur-3) var(--ease); }
