/* KHOORK Skin */
:root{
  --k-gold:#d4af37;
  --k-gold-deep:#b38f21;
  --k-ink:#0b0b0b;
  --k-panel:#121212;
  --k-muted:#1a1a1a;
  --k-border:#2a2a2a;
  --k-text:#f1f1f1;
}

.kie-theme-khoork{background:linear-gradient(180deg,#111 0%, #0b0b0b 100%); color:var(--k-text); border:1px solid var(--k-border)}
.kie-header{display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;padding:8px 10px;border-radius:10px;background:linear-gradient(180deg,#161616 0%, #0d0d0d 100%);border:1px solid var(--k-border)}
.kie-header h2{margin:0;font:800 18px/1.2 system-ui; letter-spacing:.2px}

.kie-brand{display:flex;align-items:center;gap:12px}
.khoork-logo{font:900 22px/1 system-ui; letter-spacing:.8px; color:var(--k-text)}
.khoork-logo span{display:inline-block; width:1ch; text-align:center; color:#000; background:radial-gradient(circle at 50% 55%, #ffd966 0%, var(--k-gold) 60%, #8a6b12 100%); border-radius:50%; padding:0 .15ch; box-shadow:0 0 0 2px #d4af37aa inset}
.khoork-tag{font:600 11px/1.1 system-ui; color:#c9c9c9; opacity:.9}

.kie-load{display:flex;gap:8px;align-items:center}
#kie-url{flex:1;min-width:260px;padding:8px;border:1px solid var(--k-border);border-radius:8px;background:#0f0f0f;color:var(--k-text)}
.kie-or{opacity:.6}
.kie-file input{display:none}
.kie-file span{display:inline-block;padding:8px 12px;border:1px dashed var(--k-border);border-radius:8px;background:#0f0f0f;color:var(--k-text);cursor:pointer}
.kie-btn{padding:8px 12px;border:1px solid var(--k-border);background:#111;border-radius:8px;cursor:pointer;color:var(--k-text)}
.kie-btn.primary{background:var(--k-gold);border-color:var(--k-gold-deep);color:#111;font-weight:900}
.kie-btn.lite{background:#151515}

.kie-layout{display:grid;grid-template-columns:300px 1fr;gap:12px}
.kie-left{border:1px solid var(--k-border);border-radius:10px;padding:12px;background:var(--k-panel)}
.kie-center{border:1px solid var(--k-border);border-radius:10px;padding:12px;background:#0f0f0f}

.kie-tool-title{font-weight:800;margin-bottom:6px;color:#ddd}
.kie-tool{display:inline-block;margin:2px 4px;padding:8px 10px;border:1px solid var(--k-border);border-radius:8px;background:#111;color:#eee;cursor:pointer}
.kie-tool.active{outline:2px solid var(--k-gold)}

.kie-tools label{display:flex;align-items:center;gap:8px;margin:6px 0}
.kie-tools input[type="range"]{flex:1}
.kie-tools .val{min-width:48px;text-align:right;color:#bdbdbd}

.kie-stage{position:relative;min-height:420px}
.kie-canvas-wrap{position:relative;display:block;max-width:100%;overflow:auto;border:1px dashed var(--k-border);border-radius:10px;background:repeating-conic-gradient(#222 0% 25%, #1a1a1a 0% 50%) 50%/16px 16px}
#kie-canvas-base,#kie-canvas-draw,#kie-canvas-text,#kie-canvas-mask{display:block;width:100%;height:auto}
#kie-canvas-base{position:relative;z-index:1}
#kie-canvas-draw{position:absolute;left:0;top:0;z-index:2;pointer-events:none}
#kie-canvas-text{position:absolute;left:0;top:0;z-index:3;pointer-events:none}
#kie-canvas-mask{position:absolute;left:0;top:0;z-index:4;pointer-events:auto}

.kie-hints{font-size:12px;color:#c9c9c9;margin-top:6px}

.kie-bottombar .kie-row{display:flex;flex-wrap:wrap;gap:12px;align-items:center}
#trans-opts,#brush-opts,#text-opts{display:none}
#trans-opts.active,#brush-opts.active,#text-opts.active{display:flex;flex-wrap:wrap;gap:10px;align-items:center}

.kie-adv{margin-top:12px}
.kie-adv summary{cursor:pointer;font-weight:800}
#kie-api-example{display:block;margin:8px 0;padding:8px;background:#0f0f0f;border:1px solid var(--k-border);border-radius:8px;font-size:12px;overflow:auto;color:#ddd}

/* Help overlay */
.kie-help-overlay{position:fixed;inset:0;background:rgba(0,0,0,.6);backdrop-filter:blur(2px);display:grid;place-items:center;z-index:9999}
.kie-help-card{width:min(520px,92vw);background:#0f0f0f;border:1px solid var(--k-border);border-radius:12px;padding:16px}
.kie-help-card h3{margin:6px 0 10px;font:900 18px/1.2 system-ui;color:var(--k-gold)}
.kie-help-card ul{columns:2;gap:16px;margin:0 0 10px 18px}
.khoork-logo.small{font:900 18px/1 system-ui;margin-bottom:6px}
.khoork-logo.small span{background:radial-gradient(circle at 50% 55%, #ffd966 0%, var(--k-gold) 60%, #8a6b12 100%); color:#000; border-radius:50%; padding:0 .15ch; box-shadow:0 0 0 2px #d4af37aa inset}

/* Compare panel */
.kie-compare{position:fixed;inset:auto 16px 16px 16px;background:#0f0f0f;border:1px solid var(--k-border);border-radius:12px;padding:12px;z-index:9998}
.kie-compare-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:8px}
.kie-compare-body{display:flex;gap:12px;flex-wrap:wrap}
.kie-compare .kie-half{flex:1;min-width:320px}
.kie-compare h4{text-align:center;margin:6px 0;color:#ddd}
.kie-compare canvas{width:100%;border:1px solid var(--k-border);border-radius:8px;background:transparent}

/* Responsive */
@media (max-width:980px){
  .kie-layout{grid-template-columns:1fr}
}

/* Ensure Help overlay is always on top and clickable */
#kie-help-overlay {
  position: fixed !important;
  inset: 0 !important;
  background: rgba(0,0,0,0.72) !important;
  display: flex !important;
  align-items: center;
  justify-content: center;
  z-index: 999999 !important;
  pointer-events: auto !important;
}

#kie-help-overlay[hidden] {
  display: none !important;
}

#kie-help-overlay .kie-help-card {
  position: relative;
  background: #111;
  border: 2px solid #d4af37;
  border-radius: 12px;
  padding: 24px 32px;
  color: #fff;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6);
  z-index: 1000000;
}

#kie-help-overlay .kie-btn.primary {
  background: #d4af37;
  color: #000;
  font-weight: 700;
  margin-top: 14px;
  border-radius: 6px;
  padding: 6px 16px;
}

/* ===== KHOORK: Compact Text panel (surgical addition) ===================== */
#text-opts{
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 12px;
  row-gap: 8px;
  align-items: center;
  padding: 8px 0;
}
#text-opts label{
  display: grid;
  grid-template-columns: 90px minmax(0,1fr);
  align-items: center;
  gap: 8px;
  font-size: 13px;
  line-height: 1.2;
  color: var(--kie-muted, #cfcfcf);
}
#text-opts input[type="text"],
#text-opts input[type="number"],
#text-opts input[type="color"],
#text-opts select{
  height: 30px;
  padding: 4px 8px;
  font-size: 13px;
  background: #181818;
  border: 1px solid #2a2a2a;
  color: #eaeaea;
  border-radius: 6px;
  width: 100%;
  box-sizing: border-box;
}
#text-opts input[type="color"]{
  padding: 0; height: 30px; min-width: 44px;
}
#text-opts input[type="checkbox"]{
  transform: scale(.9);
  margin-left: 4px;
}
#text-opts .kie-btn{
  height: 30px;
  padding: 0 10px;
  font-size: 12px;
  border-radius: 6px;
}
#text-opts .kie-btn.small{ padding: 0 10px; }

/* keep the hint under the canvas readable but unobtrusive */
.kie-hints{ font-size: 12px; opacity: .85; }
/* ===== /KHOORK: Compact Text panel ======================================= */

/* Selection feedback (cursor only—handles are drawn in canvas) */
.kie-canvas-wrap { cursor: default; }
.kie-canvas-wrap.is-dragging { cursor: grabbing; }
.kie-canvas-wrap.is-resizing-nwse { cursor: nwse-resize; }
.kie-canvas-wrap.is-resizing-nesw { cursor: nesw-resize; }
.kie-canvas-wrap.is-moving { cursor: grab; }

/* === KHOORK: Floating toolbox ======================================== */
.kie-flypanel{
  position:absolute;
  top:16px; right:16px;
  width:min(420px, 46vw);
  background:#0f0f0f;
  border:1px solid var(--k-border);
  border-radius:12px;
  box-shadow:0 14px 40px rgba(0,0,0,.55);
  z-index: 9995;
}
.kie-fly-head{
  display:flex; align-items:center; justify-content:space-between;
  padding:8px 10px;
  background:linear-gradient(180deg,#161616 0%, #0d0d0d 100%);
  border-bottom:1px solid var(--k-border);
  border-top-left-radius:12px; border-top-right-radius:12px;
  cursor:move; user-select:none;
}
.kie-fly-head strong{ color:var(--k-gold); font:800 14px/1.2 system-ui; letter-spacing:.2px }
.kie-fly-actions .kie-btn{ padding:4px 8px; border-radius:6px; font-size:12px }
.kie-fly-body{
  padding:10px;
  max-height:60vh; overflow:auto;
}

/* When floating, the native under-canvas bars stay hidden */
.kie-bottombar #trans-opts,
.kie-bottombar #brush-opts,
.kie-bottombar #text-opts{ display:none !important; }
/* ===================================================================== */

/* === KHOORK: Floating toolbox ======================================== */
.kie-flypanel{
  position:fixed;
  top:16px; right:16px;
  width:min(420px, 46vw);
  background:#0f0f0f;
  border:1px solid var(--k-border);
  border-radius:12px;
  box-shadow:0 14px 40px rgba(0,0,0,.55);
  z-index: 999999;
}
.kie-fly-head{
  display:flex; align-items:center; justify-content:space-between;
  padding:8px 10px;
  background:linear-gradient(180deg,#161616 0%, #0d0d0d 100%);
  border-bottom:1px solid var(--k-border);
  border-top-left-radius:12px; border-top-right-radius:12px;
  cursor:move; user-select:none;
}

/* header is the drag handle, except the action buttons */
.kie-fly-head { cursor: move; user-select: none; }
.kie-fly-actions { display:flex; gap:6px; }
.kie-fly-actions .kie-btn { cursor: pointer; }   /* make buttons clickable */

.kie-fly-body{ padding:10px; max-height:60vh; overflow:auto; }

/* Hide old under-canvas blocks when floating is active */
.kie-bottombar #trans-opts,
.kie-bottombar #brush-opts,
.kie-bottombar #text-opts,
.kie-bottombar #adjust-opts{ display:none !important; }

/* Optional: compact labels inside Adjust panel */
#adjust-opts label{
  display:grid; grid-template-columns:110px 1fr 60px; gap:8px; align-items:center; margin:6px 0;
}
#adjust-opts input[type="range"]{ width:100%; }
#adjust-opts .val, #v-exposure, #v-bright, #v-contrast, #v-sat, #v-blur{ text-align:right; color:#bdbdbd; }

/* KHOORK — toolbar row above canvas */
#kie-toolbar-row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: max-content;
  gap: 12px;
  align-items: start;
  margin: 10px 0 8px;
}
#kie-toolbar-row .kie-card { width: auto; min-width: 260px; }
@media (max-width: 900px){
  #kie-toolbar-row { grid-auto-flow: row; }
}

/* ===== KIE: Top toolbar row alignment & spacing ===== */
.kie-toolbar-row{
  display:flex;
  gap:12px;
  align-items:flex-start;
  margin:10px 0 6px 0;                /* sits just under header */
}

.kie-toolbar-card{
  flex:0 0 auto;                       /* content-sized */
  border:1px solid var(--k-border, #2a2a2a);
  border-radius:12px;
  padding:10px 12px;
  background:#101010;
  box-shadow:0 2px 10px rgba(0,0,0,.25);
}
.kie-toolbar-card h4{
  margin:0 0 8px 0; font-weight:700; color:#eee; letter-spacing:.2px;
}

/* make the tools group look like a single bar */
.kie-tools-inline{
  display:flex; flex-wrap:wrap; gap:8px;
}

/* keep the canvas column tight to the right of the toolbar row */
.kie-canvas-wrap{ margin-top:8px; }

/* ===== KIE: Floating toolbox docked under top row ===== */
#kie-flypanel{
  position:fixed;                      /* we’ll set left/top/width via JS */
  z-index:9999;
  right:auto; left:16px; top:140px;    /* will be updated on load/resize */
  width:360px;                         /* will be overridden by JS */
}

/* give the floating panel the same width feel as the left column */
#kie-flypanel.kie-docked-left{
  max-width:100%;
}

/* tiny polish so the yellow guideline frames line up */
.kie-card, .kie-toolbar-card{outline:1px solid rgba(212,169,60,.15); outline-offset:-6px;}

/* ===== KHOORK: Toolbar row just below header ===== */
.kie-toolbar-row{
  margin: 10px 12px 6px;
}
.kie-toolbar-inner{
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--k-border, rgba(255,255,255,.15));
  background: rgba(255,255,255,.03);
  border-radius: 10px;
  padding: 10px 12px;
}
.kie-toolbar-title{
  font-weight: 700;
  font-size: 12px;
  opacity:.9;
  margin-bottom: 6px;
}
.kie-toolbar-tools,
.kie-toolbar-history{
  display: flex;
  flex-direction: column;
}
.kie-toolbar-buttons{
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.kie-toolbar-history .kie-btn{ min-width: 90px; }

/* Keep the left column as a rail but empty it visually */
.kie-left .kie-card{ display:none; }

/* Floating panel: initial position + width tied to left rail */
#kie-flypanel{
  width: var(--kie-left-width, 320px);
  left: calc(var(--kie-left-x, 12px));
  top:  calc(var(--kie-left-top, 140px));
}
@media (max-width: 980px){
  .kie-toolbar-inner{ grid-template-columns: 1fr; }
}

/* topbar row under header */
.kie-topbar{
  display:grid; grid-template-columns: 1fr 1fr; gap:16px;
  padding:8px 16px 0;
}
.kie-topcard{
  border:1px solid var(--k-border,rgba(255,255,255,.12));
  border-radius:10px; padding:10px; background:rgba(0,0,0,.25);
}
.kie-topcard-title{ font-weight:700; margin:0 0 6px 2px; }
.kie-top-tools-row,.kie-top-history-row{ display:flex; gap:8px; align-items:center; flex-wrap:wrap; }

/* keep left column as a rail but hide legacy controls */
.kie-left{ width:320px; min-width:320px; }
.kie-left .kie-tools, .kie-left .kie-tool-group { display:none; }

/* floating panel default look aligns visually with left rail */
.kie-flypanel{ z-index:9999; }

/* === KHOORK: align top Tools/History row with canvas (right of left rail) === */
:root{
  --kie-left-width: 300px;  /* your .kie-layout left column */
  --kie-gap: 12px;          /* the grid gap between columns */
  --kie-header-pad: 10px;   /* header horizontal padding (already in your CSS) */
}

/* move the whole topbar to start where the center column starts */
.kie-topbar{
  padding: 8px var(--kie-header-pad) 0 var(--kie-header-pad);
  margin-left: calc(var(--kie-left-width) + var(--kie-gap));
  width: calc(100% - (var(--kie-left-width) + var(--kie-gap) + var(--kie-header-pad)));
  box-sizing: border-box;
}

/* mobile: no left rail, so flush with page padding */
@media (max-width: 980px){
  .kie-topbar{
    margin-left: var(--kie-header-pad);
    width: calc(100% - (2 * var(--kie-header-pad)));
  }
}

/* Let JS set the exact left/right; keep header's horizontal padding */
.kie-topbar{
  padding:8px 10px 0;   /* matches .kie-header side padding */
  margin-left:0 !important;
  margin-right:0 !important;
  width:auto !important;
  box-sizing:border-box;
}

/* === KHOORK: Progress Modal for "USE IN DESIGNER" ===================== */
#kie-progress-modal {
  position: fixed;
  inset: 0;
  z-index: 100000;
  display: grid;
  place-items: center;
}
#kie-progress-modal[hidden] { display: none; }

#kie-progress-modal .kpm-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(2px);
}

#kie-progress-modal .kpm-card {
  position: relative;
  width: min(420px, 92vw);
  background: #0f0f0f;
  border: 1px solid var(--k-border, #2a2a2a);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,.55);
  padding: 16px 16px 12px;
  color: var(--k-text, #f1f1f1);
  text-align: center;
}

.kpm-body { padding: 8px 6px 12px; }
.kpm-title { font: 800 14px/1.2 system-ui; color: var(--k-gold, #d4af37); }
.kpm-msg { margin-top: 6px; opacity: .9; font-size: 12px; }

.kpm-actions { margin-top: 10px; }
.kpm-actions .kie-btn.primary {
  background: var(--k-gold, #d4af37);
  color: #111;
  border: 1px solid var(--k-gold-deep, #b38f21);
  font-weight: 800;
  border-radius: 8px;
  padding: 6px 14px;
}

/* spinner */
.kpm-spinner {
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,.15);
  border-top-color: var(--k-gold, #d4af37);
  margin: 6px auto 10px;
  animation: kpm-spin 0.9s linear infinite;
}
@keyframes kpm-spin { to { transform: rotate(360deg); } }

/* state tints (optional subtle borders) */
.kpm-card.is-success { border-color: rgba(78, 200, 120, .45); }
.kpm-card.is-error   { border-color: rgba(230, 85, 85, .45); }


/* ===== MOBILE LAYOUT (≤ 900px) ========================================= */
@media (max-width: 900px){

  /* Header compacts into two rows */
  .kie-header{
    flex-wrap: wrap;
    gap: 8px 10px;
    padding: 10px;
  }
  .kie-brand{ order: 1; }
  .kie-load{
    order: 2;
    width: 100%;
    display: grid;
    grid-template-columns: 1fr max-content max-content max-content max-content;
    gap: 8px;
  }
  #kie-url{ min-width: 0; width: 100%; }
  .kie-or{ display: none; } /* saves space on phones */

  /* Layout: single column, left rail collapses visually */
  .kie-layout{
    grid-template-columns: 1fr;      /* single column */
    gap: 10px;
  }
  .kie-left{
    border: 0;
    padding: 0;
    background: transparent;
    width: 0; min-width: 0;
    height: 0; min-height: 0;
    overflow: hidden;
  }

  /* Top tools row: becomes a pill card with horizontal scroll */
  .kie-topbar{
    grid-template-columns: 1fr;       /* stack Tools + History */
    gap: 10px;
    padding: 0 10px;
  }
  .kie-topcard{
    padding: 10px;
    border-radius: 10px;
    background: rgba(255,255,255,.04);
  }
  .kie-top-tools-row{
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }
  .kie-top-tools-row .kie-tool,
  .kie-top-history-row .kie-btn{
    flex: 0 0 auto;                   /* no wrap, scroll instead */
  }

  /* Stage height on phones */
  .kie-stage{ min-height: 52vh; }
  .kie-canvas-wrap{
    max-height: 62vh;
    border-radius: 10px;
  }

  /* Floating toolbox: full-width modal-ish card pinned under header */
  .kie-flypanel{
    position: fixed !important;
    left: 12px !important;
    right: 12px !important;
    width: auto !important;
    top: calc(env(safe-area-inset-top, 0px) + 120px) !important; /* under header */
    z-index: 99999;
  }
  .kie-fly-body{ max-height: 48vh; overflow: auto; }

  /* Buttons a bit taller for touch */
  .kie-btn, .kie-tool{
    padding: 10px 12px;
  }
}

/* ===== SMALL PHONES (≤ 390px) — tighten a bit ========================= */
@media (max-width: 390px){
  .kie-load{
    grid-template-columns: 1fr max-content max-content;
  }
  #kie-export{ grid-column: 2 / span 2; }
  #kie-help{ justify-self: end; }
}

/* Mobile: let the canvas consume gestures instead of the page */
.kie-canvas-wrap,
#kie-canvas-base,
#kie-canvas-draw,
#kie-canvas-text,
#kie-canvas-mask{
  touch-action: none;   /* critical for iOS/Safari & Chrome Android */
}