/**
 * Code Kids — Code Kids Keyboard prototype
 * Stage 7.1C.2
 *
 * Landscape-first. Portrait remains unchanged.
 */

.ck-keyboard {
  display: none;
}

@media (orientation: landscape) and (max-height: 520px) and (max-width: 900px) {
  body.ck-custom-keyboard-active #code-input {
    caret-color: #f8fafc;
    user-select: text;
    -webkit-user-select: text;
  }

  .ck-keyboard {
    position: sticky;
    bottom: max(2px, env(safe-area-inset-bottom));
    z-index: 34;
    display: grid;
    gap: 4px;
    width: 100%;
    margin-top: 7px;
    padding: 5px;
    border: 1px solid rgba(99, 102, 241, 0.18);
    border-radius: 14px;
    background: rgba(238, 242, 255, 0.97);
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.12);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }

  .ck-keyboard[hidden] {
    display: none !important;
  }

  .ck-keyboard__row {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    gap: 4px;
    min-width: 0;
  }

  .ck-key {
    min-width: 0;
    min-height: clamp(33px, 10vh, 38px);
    padding: 3px 5px;
    border: 1px solid rgba(148, 163, 184, 0.28);
    border-radius: 9px;
    background: #ffffff;
    color: #1e293b;
    box-shadow: 0 2px 5px rgba(15, 23, 42, 0.07);
    font: 800 clamp(0.72rem, 2.2vw, 0.9rem)/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    white-space: nowrap;
    touch-action: manipulation;
  }

  .ck-key:active {
    transform: translateY(1px);
    box-shadow: none;
  }

  .ck-key--language {
    border-color: rgba(79, 70, 229, 0.24);
    background: #eef2ff;
    color: #3730a3;
  }

  .ck-key--value {
    border-color: rgba(79, 70, 229, 0.34);
    background: #4f46e5;
    color: #ffffff;
  }

  .ck-key--syntax {
    color: #475569;
  }

  .ck-key--utility {
    background: #f8fafc;
    color: #334155;
  }

  .ck-key--mode {
    background: #e2e8f0;
    color: #1e293b;
  }

  .ck-key--wide {
    min-width: 54px;
  }

  .ck-keyboard--system {
    position: fixed;
    right: max(8px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    left: auto;
    width: auto;
    min-width: 0;
    margin: 0;
    padding: 4px;
    border-radius: 12px;
  }

  .ck-keyboard__return {
    min-height: 36px;
    padding: 6px 11px;
    border: 1px solid rgba(99, 102, 241, 0.26);
    border-radius: 9px;
    background: #4f46e5;
    color: #ffffff;
    font: 800 0.78rem/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    white-space: nowrap;
    touch-action: manipulation;
  }

  /*
   * Prototype only: when our keyboard is active, avoid the 7.1B
   * "system keyboard open" compact rules because Android IME is closed.
   */
  body.ck-custom-keyboard-active {
    --ck-code-kb-visible: 1;
  }

  body.ck-custom-keyboard-active .code-panel {
    scroll-margin-bottom: 4px;
  }
}

/* ==========================================================
   Code Kids — 7.1C.3 Side Keyboard refinements
   ========================================================== */

@media (orientation: landscape) and (max-height: 520px) and (max-width: 900px) {
  .ck-keyboard--custom {
    position: relative;
    inset: auto;
    align-self: stretch;
    width: 100%;
    height: 100%;
    min-width: 0;
    margin: 0;
    padding: 5px;
    overflow: hidden;
    border-radius: 14px;
  }

  .ck-keyboard__handle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-height: 27px;
    margin-bottom: 1px;
    border: 1px dashed rgba(99, 102, 241, 0.30);
    border-radius: 9px;
    background: rgba(255, 255, 255, 0.66);
    color: #64748b;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
  }

  .ck-keyboard__handle:active,
  .ck-keyboard--dragging .ck-keyboard__handle {
    cursor: grabbing;
  }

  .ck-keyboard__grip {
    font-size: 1rem;
    font-weight: 900;
    letter-spacing: -0.18em;
    transform: rotate(90deg);
  }

  .ck-keyboard__handle-label {
    font-size: 0.58rem;
    font-weight: 800;
    letter-spacing: 0.035em;
    text-transform: uppercase;
  }

  .ck-keyboard--custom .ck-keyboard__row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-flow: row;
    grid-auto-columns: auto;
    gap: 4px;
    min-height: 0;
  }

  .ck-keyboard--custom .ck-key {
    min-height: 34px;
    padding: 2px 4px;
    border-radius: 8px;
    font-size: clamp(0.68rem, 1.9vw, 0.82rem);
  }

  .ck-keyboard--custom .ck-key--wide {
    min-width: 0;
  }

  .ck-keyboard--dragging {
    z-index: 90;
    pointer-events: none;
    box-shadow: 0 16px 34px rgba(15, 23, 42, 0.22);
    will-change: transform;
  }

  .ck-keyboard--dragging .ck-keyboard__handle {
    pointer-events: auto;
  }

  .ck-keyboard--snapping {
    will-change: transform;
  }

  .ck-dock-zones {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: none;
    grid-template-columns: 1fr 1fr;
    pointer-events: none;
  }

  .ck-dock-zones.is-visible {
    display: grid;
  }

  .ck-dock-zone {
    display: flex;
    align-items: center;
    padding: 10px;
    color: rgba(79, 70, 229, 0.50);
    font-size: 0.66rem;
    font-weight: 900;
    letter-spacing: 0.06em;
  }

  .ck-dock-zone--left {
    justify-content: flex-start;
  }

  .ck-dock-zone--right {
    justify-content: flex-end;
  }

  .ck-dock-zones[data-target="left"] .ck-dock-zone--left,
  .ck-dock-zones[data-target="right"] .ck-dock-zone--right {
    background: rgba(99, 102, 241, 0.07);
    color: rgba(79, 70, 229, 0.92);
  }
}

/* ==========================================================
   Code Kids — 7.1C.3B
   Portrait Code Kids Keyboard
   ========================================================== */

@media (orientation: portrait) and (max-width: 699px) {
  #screen-game #code-kids-keyboard.ck-keyboard--custom {
    position: static;
    display: grid;
    gap: 5px;
    width: 100%;
    margin: 0;
    padding: 6px;
    border: 1px solid rgba(99, 102, 241, 0.18);
    border-radius: 16px;
    background: rgba(238, 242, 255, 0.97);
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.10);
  }

  #screen-game #code-kids-keyboard[hidden] {
    display: none !important;
  }

  #screen-game #code-kids-keyboard .ck-keyboard__handle {
    display: none !important;
  }

  #screen-game #code-kids-keyboard .ck-keyboard__row {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    gap: 5px;
    min-width: 0;
  }

  #screen-game #code-kids-keyboard .ck-key {
    min-width: 0;
    min-height: 42px;
    padding: 4px 5px;
    border: 1px solid rgba(148, 163, 184, 0.28);
    border-radius: 10px;
    background: #ffffff;
    color: #1e293b;
    box-shadow: 0 2px 5px rgba(15, 23, 42, 0.06);
    font: 800 clamp(0.68rem, 3vw, 0.9rem)/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    white-space: nowrap;
    touch-action: manipulation;
  }

  #screen-game #code-kids-keyboard .ck-key--language {
    border-color: rgba(79, 70, 229, 0.24);
    background: #eef2ff;
    color: #3730a3;
  }

  #screen-game #code-kids-keyboard .ck-key--value {
    border-color: rgba(79, 70, 229, 0.34);
    background: #4f46e5;
    color: #ffffff;
  }

  #screen-game #code-kids-keyboard .ck-key--utility {
    background: #f8fafc;
    color: #334155;
  }

  #screen-game #code-kids-keyboard .ck-key--mode {
    background: #e2e8f0;
    color: #1e293b;
  }

  #screen-game #code-kids-keyboard[data-profile="PROGRAM_FIRST"] .ck-key {
    min-height: 38px;
    font-size: clamp(0.62rem, 2.65vw, 0.82rem);
  }

  #screen-game #code-kids-keyboard.ck-keyboard--system {
    position: fixed;
    right: max(8px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    left: auto;
    z-index: 95;
    display: block;
    width: auto;
    margin: 0;
    padding: 4px;
  }

  #screen-game #code-kids-keyboard .ck-keyboard__return {
    min-height: 40px;
    padding: 7px 12px;
    border: 0;
    border-radius: 10px;
    background: #4f46e5;
    color: #ffffff;
    font-weight: 800;
  }
}
