/* =====================================================================================================
   Keycap buttons (default; Display → Buttons → Flat turns them off). From the keys.smhcoders.click kit,
   rebuilt for a dense HMI: one element per button, no extra markup, so every button the app generates
   (faceplates, studio steps, display menu…) gets it for free.

     button                skirt: lit from the top-left, side wall below (box-shadow), real travel
     button::before        dished top face
     button::after         specular spot that follows the pointer (keycaps.js sets --mx / --my)

   Each theme sets the key palette (--kc-*); classes the app already uses pick a variant:
   .go start · .stop stop · .estop red key in a yellow collar · .learn* accent · .on / .chip.train.sel latched.
   Colours, borders and shadows are !important so the flat theme rules underneath can stay as they are;
   states only change variables. Library cards (.card) are tiles, not keys, and are left alone.
   ===================================================================================================== */

body.keys {
  --kc-depth: 4px;
  --kc-r: 7px;
  /* dark theme: slate keys */
  --kc-hi: #566273; --kc-mid: #323c49; --kc-lo: #1b222b;
  --kc-f1: #3b4654; --kc-f2: #2b3440; --kc-side: #0e1318; --kc-tx: #e3e9ef;
  --kc-glow: rgba(90, 176, 255, .35); --kc-focus: #5ab0ff;
}
body.keys.theme-scada {
  --kc-r: 4px;
  /* SCADA: neutral grey keys, colour only for state (ISA-101) */
  --kc-hi: #888e94; --kc-mid: #60666c; --kc-lo: #383c40;
  --kc-f1: #6b7177; --kc-f2: #585e64; --kc-side: #1c1f22; --kc-tx: #eef2f4;
  --kc-glow: rgba(127, 216, 239, .28); --kc-focus: #7fd8ef;
}
body.keys.theme-smh {
  --kc-r: 6px;
  /* SMHcoders: paper keys */
  --kc-hi: #ffffff; --kc-mid: #e2dfd6; --kc-lo: #a6a296;
  --kc-f1: #f8f7f3; --kc-f2: #e2dfd6; --kc-side: #96928a; --kc-tx: #0c1517;
  --kc-glow: rgba(12, 21, 23, .22); --kc-focus: #0e9e96;
}

/* ---- variants ---- */
/* start / next */
body.keys button.go {
  --kc-hi: #46b977; --kc-mid: #1f7a46; --kc-lo: #0e3d22; --kc-f1: #278a51; --kc-f2: #1b6a3c; --kc-side: #0a2716; --kc-tx: #eafff2; --kc-glow: rgba(47, 191, 113, .4);
}
body.keys.theme-scada button.go { --kc-hi: #888e94; --kc-mid: #60666c; --kc-lo: #383c40; --kc-f1: #6b7177; --kc-f2: #585e64; --kc-side: #1c1f22; --kc-tx: #8ff0b0; --kc-glow: rgba(127, 224, 160, .3); }
body.keys.theme-smh button.go {
  --kc-hi: #4a5759; --kc-mid: #1c2729; --kc-lo: #060b0c; --kc-f1: #253335; --kc-f2: #131d1f; --kc-side: #020506; --kc-tx: #f3f2ee; --kc-glow: rgba(14, 158, 150, .4);
}
/* stop */
body.keys button.stop {
  --kc-hi: #c95a61; --kc-mid: #8a2a2f; --kc-lo: #481215; --kc-f1: #9a3338; --kc-f2: #7a2429; --kc-side: #2c0b0d; --kc-tx: #ffe9ea; --kc-glow: rgba(229, 72, 77, .4);
}
body.keys.theme-scada button.stop { --kc-hi: #888e94; --kc-mid: #60666c; --kc-lo: #383c40; --kc-f1: #6b7177; --kc-f2: #585e64; --kc-side: #1c1f22; --kc-tx: #ffb0b0; --kc-glow: rgba(255, 154, 154, .3); }
body.keys.theme-smh button.stop {
  --kc-hi: #ffad6b; --kc-mid: #f07b1e; --kc-lo: #a3480b; --kc-f1: #f78d36; --kc-f2: #de6a12; --kc-side: #8a3a06; --kc-tx: #fff8f1; --kc-glow: rgba(240, 123, 30, .45);
}
/* accent: the learning studio */
body.keys :is(button.learnhdr, button.learnbtn, button.learnmini) {
  --kc-hi: #5b9be0; --kc-mid: #2f6db5; --kc-lo: #163a66; --kc-f1: #3778c3; --kc-f2: #285fa0; --kc-side: #0d2546; --kc-tx: #ffffff; --kc-glow: rgba(90, 176, 255, .45);
}
body.keys.theme-scada :is(button.learnhdr, button.learnbtn, button.learnmini) { --kc-hi: #888e94; --kc-mid: #60666c; --kc-lo: #383c40; --kc-f1: #6b7177; --kc-f2: #585e64; --kc-side: #1c1f22; --kc-tx: #bfeefa; }
body.keys.theme-smh :is(button.learnhdr, button.learnbtn, button.learnmini) {
  --kc-hi: #46c9c0; --kc-mid: #0e9e96; --kc-lo: #075e59; --kc-f1: #1db0a7; --kc-f2: #0b8981; --kc-side: #05504c; --kc-tx: #effffd; --kc-glow: rgba(14, 158, 150, .45);
}
/* missions: amber in dark, neutral with amber text in SCADA, orange in SMHcoders */
body.keys button.missionhdr {
  --kc-hi: #f6c75a; --kc-mid: #c8901a; --kc-lo: #6e4d08; --kc-f1: #d39c22; --kc-f2: #b07f14; --kc-side: #4a3305; --kc-tx: #1b1405; --kc-glow: rgba(240, 179, 30, .45);
  font-weight: 700; text-shadow: 0 1px 0 rgba(255,255,255,.25);
}
body.keys.theme-scada button.missionhdr { --kc-hi: #888e94; --kc-mid: #60666c; --kc-lo: #383c40; --kc-f1: #6b7177; --kc-f2: #585e64; --kc-side: #1c1f22; --kc-tx: #f2c200; text-shadow: 0 1px 0 rgba(0,0,0,.3); }
body.keys.theme-smh button.missionhdr {
  --kc-hi: #ffad6b; --kc-mid: #f07b1e; --kc-lo: #a3480b; --kc-f1: #f78d36; --kc-f2: #de6a12; --kc-side: #8a3a06; --kc-tx: #fff8f1; --kc-glow: rgba(240, 123, 30, .45); text-shadow: 0 1px 0 rgba(0,0,0,.2);
}
/* selected / latched: rests part-way down */
body.keys :is(button.on, button.chip.train.sel) {
  --kc-hi: #5b9be0; --kc-mid: #2f6db5; --kc-lo: #163a66; --kc-f1: #3170b8; --kc-f2: #285fa0; --kc-side: #0d2546; --kc-tx: #ffffff;
}
body.keys.theme-scada :is(button.on, button.chip.train.sel) {
  --kc-hi: #3c4044; --kc-mid: #2a2d30; --kc-lo: #17191b; --kc-f1: #26292c; --kc-f2: #1f2225; --kc-side: #0b0c0d; --kc-tx: #7fd8ef;
}
body.keys.theme-smh :is(button.on, button.chip.train.sel) {
  --kc-hi: #4a5759; --kc-mid: #1c2729; --kc-lo: #060b0c; --kc-f1: #223032; --kc-f2: #131d1f; --kc-side: #020506; --kc-tx: #f3f2ee;
}
/* emergency stop: red key in a yellow collar, every theme */
body.keys button.estop {
  --kc-hi: #ff6470; --kc-mid: #c8102e; --kc-lo: #6a0715; --kc-f1: #d9223c; --kc-f2: #b20d27; --kc-side: #45040f; --kc-tx: #ffffff; --kc-glow: rgba(229, 16, 46, .55);
  --kc-ring: 0 1px 0 4px #f2c200, 0 2px 0 5px #8a6d00;
  margin: 0 2px;
  font-weight: 700;
}
body.keys .l-dots button { --kc-r: 50%; }

/* ---- the key ---- */
body.keys button:not(.card) {
  --kc-ty: 0px;
  --kc-sd: var(--kc-depth);
  position: relative;
  isolation: isolate;
  color: var(--kc-tx) !important;
  border-color: transparent !important;
  border-radius: var(--kc-r) !important;
  background:
    linear-gradient(90deg, rgba(255,255,255,.12), rgba(255,255,255,0) 25%, rgba(0,0,0,0) 72%, rgba(0,0,0,.18)),
    linear-gradient(180deg, var(--kc-hi) 0%, var(--kc-mid) 45%, var(--kc-lo) 100%) !important;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.3),
    inset 1px 0 1px rgba(255,255,255,.12),
    inset -1px -1px 2px rgba(0,0,0,.28),
    0 var(--kc-sd) 0 var(--kc-side),
    0 calc(var(--kc-sd) + 1px) 0 rgba(0,0,0,.3),
    0 calc(var(--kc-sd) + 2px) calc(var(--kc-sd) + 4px) rgba(0,0,0,.35),
    var(--kc-ring, 0 0 #0000) !important;
  text-shadow: 0 1px 0 rgba(0,0,0,.25);
  transform: translateY(var(--kc-ty));
  /* release springs back with a little overshoot */
  transition: transform .3s cubic-bezier(.34, 1.7, .5, 1), box-shadow .3s cubic-bezier(.34, 1.5, .5, 1), filter .2s;
  -webkit-tap-highlight-color: transparent;
}
body.keys.theme-smh button:not(.card):not(.go):not(.on):not(.sel):not(.stop):not(.estop):not(.learnhdr):not(.learnbtn):not(.learnmini) { text-shadow: 0 1px 0 rgba(255,255,255,.7); }

/* dished top face (replaces the SMHcoders theme's status dot on the RO chips) */
body.keys button:not(.card)::before {
  content: "" !important;
  position: absolute !important;
  display: block !important;
  inset: 1px 3px 3px 1px !important;
  width: auto !important; height: auto !important; margin: 0 !important;
  z-index: -1;
  border-radius: max(1px, calc(var(--kc-r) - 2px)) !important;
  background: linear-gradient(155deg, var(--kc-f1), var(--kc-f2)) !important;
  box-shadow: inset 1px 1px 1px rgba(255,255,255,.22), inset -1px -1px 2px rgba(0,0,0,.14), 0 0 2px .5px var(--kc-f2);
  pointer-events: none;
  transition: box-shadow .2s;
}
body.keys .l-dots button::before { border-radius: 50% !important; }
/* specular spot that follows the pointer */
body.keys button:not(.card)::after {
  content: "";
  position: absolute;
  inset: 1px 3px 3px 1px;
  z-index: -1;
  border-radius: max(1px, calc(var(--kc-r) - 2px));
  background: radial-gradient(circle at var(--mx, 35%) var(--my, 25%), rgba(255,255,255,.3), rgba(255,255,255,.06) 40%, rgba(255,255,255,0) 65%);
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
}
body.keys .l-dots button::after { border-radius: 50%; }

/* hover: lifts a pixel, glows in its own colour, the light follows the pointer */
@media (hover: hover) {
  body.keys button:not(.card):not(:disabled):not(.on):not(.sel):not(.active):hover:not(:active):not(.kc-down) {
    --kc-ty: -1px;
    --kc-sd: calc(var(--kc-depth) + 1px);
    box-shadow:
      inset 0 1px 0 rgba(255,255,255,.3),
      inset 1px 0 1px rgba(255,255,255,.12),
      inset -1px -1px 2px rgba(0,0,0,.28),
      0 var(--kc-sd) 0 var(--kc-side),
      0 calc(var(--kc-sd) + 1px) 0 rgba(0,0,0,.3),
      0 calc(var(--kc-sd) + 3px) calc(var(--kc-sd) + 8px) var(--kc-glow),
      var(--kc-ring, 0 0 #0000) !important;
  }
  body.keys button:not(.card):not(:disabled):hover::after { opacity: 1; }
}

/* latched: selected tabs, segments, studio steps, the selected RO unit */
body.keys :is(button.on, button.chip.train.sel):not(.card) {
  --kc-ty: calc(var(--kc-depth) * .55);
  --kc-sd: calc(var(--kc-depth) * .45);
}
body.keys :is(button.on, button.chip.train.sel):not(.card)::before {
  box-shadow: inset 1px 2px 3px rgba(0,0,0,.22), inset -1px -1px 1px rgba(255,255,255,.1), 0 0 2px .5px var(--kc-f2);
}

/* pressed: 60 ms to the bottom, the side wall collapses, the face dishes in */
body.keys button:not(.card):not(:disabled):active,
body.keys button:not(.card).kc-down {
  --kc-ty: calc(var(--kc-depth) - 1px);
  --kc-sd: 1px;
  transition-duration: .06s;
  transition-timing-function: ease-out;
}
body.keys button:not(.card):not(:disabled):active::before,
body.keys button:not(.card).kc-down::before {
  box-shadow: inset 1px 2px 3px rgba(0,0,0,.25), inset -1px -1px 1px rgba(255,255,255,.1), 0 0 2px .5px var(--kc-f2);
}

/* E-STOP latched: stays down and pulses */
body.keys button.estop.active { --kc-ty: calc(var(--kc-depth) - 1px); --kc-sd: 1px; animation: kc-estop .8s steps(2) infinite; }
@keyframes kc-estop { 50% { filter: brightness(1.45); } }

body.keys button:not(.card):disabled { filter: grayscale(.6); --kc-ty: calc(var(--kc-depth) * .3); --kc-sd: calc(var(--kc-depth) * .7); }
body.keys button:not(.card):focus-visible { outline: 2px solid var(--kc-focus); outline-offset: 2px; }

/* ---- layout room for the side wall ---- */
body.keys header button:not(.card) { margin-bottom: var(--kc-depth); }
body.keys header button.estop { margin-bottom: calc(var(--kc-depth) + 2px); }
body.keys .seg { gap: 4px; }
body.keys .seg button { margin-left: 0; }
body.keys .d-row .seg { row-gap: calc(var(--kc-depth) + 4px); }
body.keys .l-steps { gap: calc(var(--kc-depth) + 3px); }
body.keys .l-parts, body.keys .l-dots { row-gap: calc(var(--kc-depth) + 4px); }
body.keys .al-head button { margin-bottom: 2px; }
body.keys .l-head > button { align-self: flex-start; }       /* "Back to plant" stretched to the title's height */

/* keyboard shortcuts in the help read as tiny keys too */
body.keys kbd {
  display: inline-block;
  padding: 1px 6px 2px;
  border: 0;
  border-radius: 4px;
  color: var(--kc-tx);
  background: linear-gradient(180deg, var(--kc-hi), var(--kc-mid) 45%, var(--kc-lo));
  box-shadow: inset 0 1px 0 rgba(255,255,255,.3), inset -1px -1px 1px rgba(0,0,0,.25), 0 2px 0 var(--kc-side), 0 3px 3px rgba(0,0,0,.3);
  margin: 0 1px 3px;
}

@media (prefers-reduced-motion: reduce) {
  body.keys button:not(.card) { transition-duration: .01s !important; }
  body.keys button.estop.active { animation: none; }
}
