/* ==========================================================================
   DecisionPilot Dashboard
   A dark, dense operations console. The visual language is deliberately
   restrained: one accent hue, flat surfaces, generous whitespace inside a
   tight grid, and colour reserved for status meaning rather than decoration.
   ========================================================================== */

:root {
    --bg:            #14161a;
    --surface:       #1c1f26;
    --surface-2:     #232732;
    --border:        #2c313d;
    --border-strong: #39404f;

    --text:          #e6e9ef;
    --text-dim:      #98a1b3;
    --text-faint:    #6b7383;

    --accent:        #4f8cff;
    --ok:            #3fb950;
    --warn:          #d29922;
    --bad:           #f04747;

    --cp:            #d7a52a;
    --cp-lo:         #8d6a14;
    --hp:            #c0392b;
    --hp-lo:         #7d2019;
    --mp:            #2f6fd0;
    --mp-lo:         #1c4a8f;

    --radius:        10px;
    --radius-sm:     6px;

    --shadow:        0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.25);

    --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
            "Helvetica Neue", Arial, sans-serif;
    --mono: "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* ── App shell ─────────────────────────────────────────────────────────── */

.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px 22px;
    background: rgba(20, 22, 26, .85);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}

.app-header .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 650;
    letter-spacing: -.01em;
    font-size: 16px;
}

.brand-mark {
    width: 22px;
    height: 22px;
    border-radius: 6px;
    background: linear-gradient(135deg, var(--accent), #7cb0ff);
    display: grid;
    place-items: center;
    font-size: 12px;
    font-weight: 800;
    color: #0d1117;
}

.app-header .spacer { flex: 1; }

.conn-pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 5px 11px;
    font-size: 12.5px;
    color: var(--text-dim);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
}

.app-main {
    max-width: 1180px;
    margin: 0 auto;
    padding: 24px 22px 72px;
}

/* ── Panels ────────────────────────────────────────────────────────────── */

.panel-stack {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    animation: panel-in .34s cubic-bezier(.16,.84,.44,1) both;
}

@keyframes panel-in {
    from { opacity: 0; transform: translateY(10px) scale(.995); }
    to   { opacity: 1; transform: none; }
}

.panel.is-offline { opacity: .62; }

.panel-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 13px 16px;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(180deg, rgba(255,255,255,.022), transparent);
}

.status-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--text-faint);
    flex: none;
    transition: background .25s ease, box-shadow .25s ease;
}
.status-dot.running { background: var(--ok); box-shadow: 0 0 0 3px rgba(63,185,80,.16); }
.status-dot.stopped { background: var(--text-faint); }
.status-dot.offline { background: var(--bad); box-shadow: 0 0 0 3px rgba(240,71,71,.14); }

.panel-title { min-width: 0; }

.panel-name {
    font-weight: 620;
    font-size: 15px;
    letter-spacing: -.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.panel-sub {
    font-size: 12px;
    color: var(--text-faint);
    font-family: var(--mono);
}

.panel-head .spacer { flex: 1; }

/* Top-aligned rather than centered: Script's box grows taller when the additional-scripts
   subtext is present, and centering it would otherwise shift its label out of line with
   Arduino's and Elapsed's labels beside it. */
.meta {
    display: flex;
    align-items: flex-start;
    gap: 16px;
}

.meta-item { text-align: right; line-height: 1.25; }

.meta-label {
    display: block;
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-faint);
}

.meta-value { font-size: 13.5px; font-variant-numeric: tabular-nums; }
.meta-value.mono { font-family: var(--mono); }

/* Additional scripts running alongside the primary one, shown as a small subtext line
   under the Script meta value so a user can see "what else is running" without opening
   the desktop app. Empty and takes no space when there are no additional scripts. */
.meta-sub {
    display: block;
    font-size: 10.5px;
    color: var(--text-faint);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 160px;
}

/* ── Panel body ────────────────────────────────────────────────────────── */

.panel-body {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
    gap: 22px;
    padding: 16px;
}

.section-label {
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--text-faint);
    margin-bottom: 9px;
}

/* ── Resource bars ─────────────────────────────────────────────────────── */

/* Modelled on the in-game HUD: a dark bevelled frame holding stacked rows,
   each with an inset track, a glossy fill, and the value centred over it. */
.bars {
    background: #0e1013;
    border: 1px solid #000;
    border-radius: var(--radius-sm);
    padding: 5px;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.03);
}

.bar-row {
    display: grid;
    grid-template-columns: 34px 1fr;
    align-items: center;
    gap: 7px;
}
.bar-row + .bar-row { margin-top: 4px; }

.bar-label {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .04em;
    color: #cfd6e4;
    text-align: center;
    text-shadow: 0 1px 1px #000;
}

.bar-track {
    position: relative;
    height: 14px;
    border-radius: 3px;
    background: #05070a;
    box-shadow: inset 0 1px 3px rgba(0,0,0,.9);
    overflow: hidden;
}

.bar-fill {
    position: absolute;
    inset: 0 auto 0 0;
    width: 0;
    border-radius: 3px 0 0 3px;
    /* The transition is what makes incoming telemetry feel continuous rather
       than snapping between discrete samples. */
    transition: width .45s cubic-bezier(.22,.75,.3,1), background-color .3s ease;
    will-change: width;
}

/* Gloss highlight, matching the game's bar shading. */
.bar-fill::after {
    content: "";
    position: absolute;
    inset: 0 0 50% 0;
    background: linear-gradient(180deg, rgba(255,255,255,.26), rgba(255,255,255,.04));
    pointer-events: none;
}

.bar-row[data-slot="cp"] .bar-fill { background: linear-gradient(180deg, var(--cp), var(--cp-lo)); }
.bar-row[data-slot="hp"] .bar-fill { background: linear-gradient(180deg, var(--hp), var(--hp-lo)); }
.bar-row[data-slot="mp"] .bar-fill { background: linear-gradient(180deg, var(--mp), #1c4a8f); }
.bar-row[data-slot="xp"] .bar-fill { background: linear-gradient(180deg, #8a5cd6, #4e2f80); }
.bar-row[data-slot="mob"] .bar-fill { background: linear-gradient(180deg, #b5563f, #6d2b1d); }
.bar-row[data-slot="generic"] .bar-fill { background: linear-gradient(180deg, #59636f, #333a44); }

/* A user-chosen colour wins over the slot default. Declared after the slot rules and scoped
   to [data-custom] so it overrides them without needing !important. The darker stop is
   derived with colour-mix so one picked value still yields the same gradient treatment. */
.bar-row[data-custom="1"] .bar-fill {
    background: linear-gradient(180deg,
        var(--bar-custom),
        color-mix(in srgb, var(--bar-custom) 55%, #000));
}

.bar-value {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    /* Sized to sit inside the shorter track: at the previous 11.5px the line box
       exceeded the track height and the digits clipped against the overflow. */
    font-size: 10.5px;
    line-height: 1;
    font-weight: 650;
    font-variant-numeric: tabular-nums;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0,0,0,.95);
    letter-spacing: .02em;
}

.bar-track.is-empty .bar-value { color: var(--text-faint); text-shadow: none; }

/* A bar whose reading has aged out (paused script, silent client). Hatched rather than
   simply blank so it reads as "no current data" instead of "genuinely zero", which
   matters because 0% HP is itself a meaningful value. */
.bar-track.is-stale {
    background-image: repeating-linear-gradient(
        135deg,
        rgba(255,255,255,.045) 0 6px,
        transparent 6px 12px);
}

.bars-empty {
    padding: 14px;
    text-align: center;
    color: var(--text-faint);
    font-size: 13px;
}

/* ── Rule activity ─────────────────────────────────────────────────────── */

.rules-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    min-height: 58px;
    align-content: flex-start;
}

/* Rule activations read as game skill tiles: a square icon that pops and flashes as it
   fires while a colour ring traces clockwise around its border and a shockwave rolls
   outward, then settles into the row and fades as newer casts arrive. */
.rule-cast {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    width: 52px;
    flex: none;
    transition: opacity .5s ease;
}

/* The frame is the un-clipped stage for the ring, glow and shockwave. The art below it
   still clips its own artwork, which is why those effects cannot live inside it. */
.rule-cast-frame {
    position: relative;
    width: 44px;
    height: 44px;
    animation: cast-enter .32s cubic-bezier(.16,.84,.44,1) both;
}

.rule-cast-art {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 8px;
    overflow: hidden;
    background: #0f1216;
    border: 1px solid var(--border-strong);
}

.rule-cast.is-casting .rule-cast-frame {
    animation: cast-enter .32s cubic-bezier(.16,.84,.44,1) both,
               cast-pop .7s cubic-bezier(.18,.9,.28,1) both;
}

.rule-cast.is-casting .rule-cast-art {
    border-color: var(--accent);
    animation: cast-glow .7s ease-out both;
}

.rule-cast-art img,
.rule-cast-art .rule-fallback {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
}

/* Overlays, so the burst layers never fight the tile's own transform. */
.rule-cast-flash,
.rule-cast-ring,
.rule-cast-shock {
    position: absolute;
    pointer-events: none;
    opacity: 0;
}

.rule-cast-flash {
    inset: 0;
    border-radius: inherit;
    background: radial-gradient(circle at 50% 50%, #fff 0 45%, rgba(255,255,255,.35) 100%);
    mix-blend-mode: screen;
}

/* A colour ring that traces clockwise around the icon's border, like a skill being
   channelled. The wedge is a conic-gradient whose angle is animated via --cast-angle;
   the two-layer mask (padding-box XOR border-box) keeps only a 3px band that follows
   the rounded-square edge exactly, rather than an inscribed circle. The leading edge is
   brightened to a white-hot head so the direction of travel is obvious. */
.rule-cast-ring {
    inset: -3px;
    border-radius: 11px;
    padding: 3px;
    background: conic-gradient(from -90deg,
        rgba(79,140,255,0) 0deg,
        rgba(79,140,255,.35) calc(var(--cast-angle) - 120deg),
        var(--accent) calc(var(--cast-angle) - 40deg),
        #eaf2ff var(--cast-angle),
        rgba(79,140,255,0) var(--cast-angle));
    -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
            mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
            mask-composite: exclude;
    filter: drop-shadow(0 0 5px var(--accent));
}

/* A single ring that expands past the tile and dissolves, giving the cast some impact
   beyond the icon's own bounds. */
.rule-cast-shock {
    inset: -3px;
    border-radius: 11px;
    border: 2px solid var(--accent);
}

.rule-cast.is-casting .rule-cast-flash { animation: cast-flash .5s cubic-bezier(.2,.7,.3,1) both; }
.rule-cast.is-casting .rule-cast-shock { animation: cast-shock .7s cubic-bezier(.1,.7,.3,1) both; }

/* The ring's duration is set per tile from the rule's cooldown, so one full clockwise lap
   equals "ready to fire again". Linear timing is deliberate: an eased sweep would misreport
   remaining cooldown at every point except the endpoints. */
.rule-cast.is-casting .rule-cast-ring {
    animation: cast-ring var(--cast-duration, .7s) linear both;
}

.rule-cast-name {
    font-size: 10.5px;
    line-height: 1.2;
    max-width: 100%;
    text-align: center;
    color: var(--text-faint);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Older activity fades rather than disappearing, so the row reads as a
   timeline at a glance instead of a flickering list. */
.rule-cast[data-age="1"] { opacity: .82; }
.rule-cast[data-age="2"] { opacity: .6; }
.rule-cast[data-age="3"] { opacity: .4; }
.rule-cast[data-age="4"] { opacity: .24; }

@keyframes cast-enter {
    from { opacity: 0; transform: translateX(-10px); }
}

/* Squash-and-stretch with a slight counter-rotation, so the tile lands rather than
   simply growing. */
@keyframes cast-pop {
    0%   { transform: scale(.62) rotate(-7deg); }
    30%  { transform: scale(1.26) rotate(4deg); }
    52%  { transform: scale(.93) rotate(-2deg); }
    74%  { transform: scale(1.05) rotate(1deg); }
    100% { transform: scale(1) rotate(0deg); }
}

@keyframes cast-glow {
    0%   { box-shadow: 0 0 0 2px var(--accent), 0 0 26px 2px var(--accent); }
    60%  { box-shadow: 0 0 0 1px var(--accent), 0 0 14px -2px var(--accent); }
    100% { box-shadow: 0 0 0 0 transparent, 0 0 0 0 transparent; }
}

@keyframes cast-flash {
    0%   { opacity: .95; transform: scale(1); }
    100% { opacity: 0; transform: scale(1.1); }
}

@keyframes cast-shock {
    0%   { opacity: .9; transform: scale(.9); }
    100% { opacity: 0; transform: scale(1.75); }
}

/* Angles are not interpolable on a plain custom property, so register it with a type.
   Browsers without @property still show the ring, just stepping to its final angle. */
@property --cast-angle {
    syntax: '<angle>';
    inherits: false;
    initial-value: 0deg;
}

/* The lap runs the full length so the sweep position maps linearly to elapsed cooldown;
   the ring only fades over the last sliver, once it has already closed. */
@keyframes cast-ring {
    0%   { opacity: 1; --cast-angle: 0deg; }
    94%  { opacity: 1; }
    100% { opacity: 0; --cast-angle: 360deg; }
}

/* Respect users who ask for reduced motion: keep the highlight, drop the movement. */
@media (prefers-reduced-motion: reduce) {
    .rule-cast-frame,
    .rule-cast.is-casting .rule-cast-frame { animation: none; }
    .rule-cast-ring,
    .rule-cast-shock { display: none; }
    .rule-cast.is-casting .rule-cast-flash { animation: cast-flash .3s ease-out both; }
}

.rule-fallback {
    display: grid;
    place-items: center;
    font-size: 13px;
    font-weight: 700;
    color: var(--accent);
    background: #0f1216;
}

.rules-empty {
    color: var(--text-faint);
    font-size: 13px;
    display: grid;
    place-items: center;
    min-height: 58px;
}

/* ── Controls ──────────────────────────────────────────────────────────── */

.panel-foot {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    background: rgba(0,0,0,.14);
}

.btn {
    appearance: none;
    border: 1px solid var(--border-strong);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-size: 13.5px;
    padding: 7px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background .15s ease, border-color .15s ease, transform .06s ease;
}
.btn:hover  { background: #2b3040; border-color: #454d5e; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: #0b1220; font-weight: 600; }
.btn-primary:hover { background: #6ea0ff; border-color: #6ea0ff; }

.btn-danger { background: transparent; border-color: #5d2b2b; color: #ff8b8b; }
.btn-danger:hover { background: rgba(240,71,71,.12); border-color: #7d3a3a; }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.btn-sm { padding: 5px 10px; font-size: 12.5px; }

.panel-foot .spacer { flex: 1; }

.preset-group { display: flex; gap: 6px; flex-wrap: wrap; }

/* ── Add panel ─────────────────────────────────────────────────────────── */

.add-card {
    margin-top: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    width: 100%;
    padding: 18px;
    background: transparent;
    border: 1.5px dashed var(--border-strong);
    border-radius: var(--radius);
    color: var(--text-dim);
    font: inherit;
    font-size: 14px;
    cursor: pointer;
    transition: border-color .18s ease, color .18s ease, background .18s ease;
}
.add-card:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(79,140,255,.06);
}

.add-plus {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1.5px solid currentColor;
    display: grid;
    place-items: center;
    font-size: 15px;
    line-height: 1;
}

.empty-state {
    text-align: center;
    padding: 56px 20px 12px;
    color: var(--text-faint);
}
.empty-state h2 {
    margin: 0 0 6px;
    font-size: 17px;
    font-weight: 600;
    color: var(--text-dim);
}
.empty-state p { margin: 0; font-size: 13.5px; }

/* ── Dialog ────────────────────────────────────────────────────────────── */

dialog.sheet {
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    padding: 0;
    width: min(440px, calc(100vw - 32px));
    box-shadow: 0 16px 48px rgba(0,0,0,.6);
}
dialog.sheet::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(2px); }

.sheet-head {
    padding: 16px 18px 0;
    font-size: 15.5px;
    font-weight: 620;
}
.sheet-body { padding: 14px 18px; }
.sheet-foot {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 0 18px 16px;
}

.field { margin-bottom: 13px; }
.field:last-child { margin-bottom: 0; }

.field label {
    display: block;
    font-size: 12px;
    color: var(--text-dim);
    margin-bottom: 5px;
}

.field input,
.field select {
    width: 100%;
    padding: 9px 11px;
    font: inherit;
    font-size: 14px;
    color: var(--text);
    background: #12151a;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.field input:focus,
.field select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79,140,255,.16);
}

.field .hint { font-size: 11.5px; color: var(--text-faint); margin-top: 5px; }

.field-error { color: #ff8b8b; font-size: 12.5px; min-height: 17px; }

/* ── Bar configuration list ─────────────────────────────────────────────────────── */

.sheet-foot-spacer { flex: 1; }

.sheet-subhead {
    font-size: 12px;
    color: var(--text-dim);
    margin: 16px 0 8px;
    padding-top: 13px;
    border-top: 1px solid var(--border);
}

.bar-config {
    display: flex;
    flex-direction: column;
    gap: 5px;
    /* Long bar lists stay inside the dialog instead of pushing the footer off-screen. */
    max-height: 260px;
    overflow-y: auto;
}

.bar-config-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    cursor: grab;
}

.bar-config-row.is-dragging { opacity: .45; cursor: grabbing; }

.bar-config-grip {
    color: var(--text-faint);
    font-size: 12px;
    letter-spacing: -2px;
    flex: none;
    user-select: none;
}

.bar-config-name {
    flex: 1;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 7px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.bar-config-tag {
    font-size: 10px;
    color: var(--text-faint);
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    padding: 1px 6px;
    flex: none;
}

.bar-config-eye,
.bar-config-clear {
    flex: none;
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 13px;
    line-height: 1;
    padding: 3px 5px;
    border-radius: 4px;
}

.bar-config-eye:hover,
.bar-config-clear:hover { background: rgba(255,255,255,.07); color: var(--text); }

.bar-config-eye.is-off { opacity: .45; }

/* Strip the native chrome so the swatch reads as a colour chip rather than a form control. */
.bar-config-color {
    flex: none;
    width: 30px;
    height: 24px;
    padding: 0;
    background: none;
    border: 1px solid var(--border-strong);
    border-radius: 4px;
    cursor: pointer;
}
.bar-config-color::-webkit-color-swatch-wrapper { padding: 2px; }
.bar-config-color::-webkit-color-swatch { border: none; border-radius: 2px; }
.bar-config-color::-moz-color-swatch { border: none; border-radius: 2px; }

/* ── Toast ─────────────────────────────────────────────────────────────── */

.toast-host {
    position: fixed;
    bottom: 18px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 60;
    pointer-events: none;
}

.toast {
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 9px 14px;
    font-size: 13.5px;
    box-shadow: var(--shadow);
    animation: toast-in .24s ease both;
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
}

/* ── Responsive ────────────────────────────────────────────────────────── */

@media (max-width: 820px) {
    .panel-body { grid-template-columns: 1fr; gap: 18px; }

    .panel-head { flex-wrap: wrap; }
    .panel-head .meta { width: 100%; justify-content: flex-start; gap: 20px; }
    .meta-item { text-align: left; }
}

@media (max-width: 520px) {
    .app-main { padding: 16px 13px 60px; }
    .app-header { padding: 12px 14px; }
    .conn-pill span.label { display: none; }

    .panel-foot .btn { flex: 1 1 auto; }
    .panel-foot .preset-group { width: 100%; }
    .panel-foot .preset-group .btn { flex: 1; }

    /* Comfortable touch targets on phones. */
    .btn { padding: 10px 14px; }
    .bar-track { height: 17px; }
}
