/**
 * Code Kids — Landscape UI 7.1C.3
 * OWNER APPROVED contract implementation.
 *
 * Landscape is a working surface:
 * Preview | Code | Side Keyboard.
 * Portrait 7.1B is intentionally untouched.
 */

@media (orientation: landscape) and (max-height: 520px) and (max-width: 900px) {
  :root {
    --ck-landscape-gap: 6px;
    --ck-landscape-radius: 14px;
    --ck-side-keyboard-width: clamp(132px, 22vw, 164px);
  }

  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  body {
    min-height: var(--ck-visual-height, 100dvh);
  }

  .app-shell {
    width: 100%;
    height: var(--ck-visual-height, 100dvh);
    min-height: 0;
    padding:
      max(5px, env(safe-area-inset-top))
      max(6px, env(safe-area-inset-right))
      max(5px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
  }

  #screen-game.active {
    display: block;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  /* Contract 7.1C.3: no large header and no large task card in landscape. */
  #screen-game .game-header,
  #screen-game .task-panel {
    display: none !important;
  }

  #screen-game .game-grid {
    display: grid;
    width: 100%;
    height: 100%;
    min-height: 0;
    gap: var(--ck-landscape-gap);
    align-items: stretch;
    align-content: stretch;
    grid-template-rows: minmax(0, 1fr);
    grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.38fr);
    grid-template-areas: "preview code";
  }

  body.ck-custom-keyboard-active.ck-keyboard-side-right #screen-game .game-grid {
    grid-template-columns:
      minmax(0, 0.92fr)
      minmax(0, 1.38fr)
      var(--ck-side-keyboard-width);
    grid-template-areas: "preview code keyboard";
  }

  body.ck-custom-keyboard-active.ck-keyboard-side-left #screen-game .game-grid {
    grid-template-columns:
      var(--ck-side-keyboard-width)
      minmax(0, 1.38fr)
      minmax(0, 0.92fr);
    grid-template-areas: "keyboard code preview";
  }

  #screen-game .preview-panel {
    grid-area: preview;
  }

  #screen-game .code-panel {
    grid-area: code;
  }

  #screen-game #code-kids-keyboard {
    grid-area: keyboard;
  }

  #screen-game .preview-panel,
  #screen-game .code-panel {
    min-width: 0;
    min-height: 0;
    height: 100%;
    padding: 8px;
    overflow: hidden;
    border-radius: var(--ck-landscape-radius);
  }

  #screen-game .preview-panel {
    display: flex;
    flex-direction: column;
  }

  #screen-game .preview-level-heading {
    flex: 0 0 auto;
    margin: 0 2px 6px;
    overflow: hidden;
    color: #475569;
    font-size: clamp(0.72rem, 2vw, 0.88rem);
    font-weight: 900;
    letter-spacing: 0;
    line-height: 1.15;
    text-overflow: ellipsis;
    text-transform: none;
    white-space: nowrap;
  }

  #screen-game .preview-stage {
    flex: 1 1 auto;
    min-height: 0;
    height: auto;
    max-height: none;
    overflow: hidden;
    border-radius: 12px;
    background-size: 18px 18px;
  }

  #screen-game .preview-object {
    max-width: min(155px, 74%);
    max-height: min(155px, 68%);
  }

  #screen-game .feedback {
    flex: 0 0 auto;
    min-height: 17px;
    margin: 5px 2px 0;
    overflow: hidden;
    color: #64748b;
    font-size: 0.69rem;
    line-height: 1.2;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #screen-game .code-panel {
    display: flex;
    flex-direction: column;
  }

  #screen-game .code-panel-topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 42px;
    gap: 6px;
    flex: 0 0 auto;
    min-height: 42px;
    margin: 0 0 7px;
  }

  #screen-game .code-panel-topbar .code-actions {
    position: static !important;
    display: grid;
    grid-template-columns: minmax(72px, 0.72fr) minmax(112px, 1.28fr);
    gap: 6px;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }

  #screen-game .code-panel-topbar .code-actions .btn,
  #screen-game .mobile-pause-slot #btn-pause {
    min-width: 0;
    min-height: 42px;
    height: 42px;
    padding: 5px 8px;
    border-radius: 11px;
    box-shadow: none;
    font-size: clamp(0.72rem, 1.95vw, 0.84rem);
    line-height: 1;
    white-space: nowrap;
  }

  #screen-game .mobile-pause-slot {
    display: flex !important;
    align-items: center;
    justify-content: center;
    width: 42px;
    min-width: 42px;
    min-height: 42px;
  }

  #screen-game .mobile-pause-slot:empty {
    display: flex !important;
  }

  #screen-game .mobile-pause-slot #btn-pause {
    width: 42px;
    flex: 0 0 42px;
    padding: 0;
    background: #eef2ff;
  }

  #screen-game #code-input {
    flex: 1 1 auto;
    width: 100%;
    min-height: 0;
    height: auto;
    max-height: none;
    resize: none;
    padding: 10px 11px;
    border-radius: 12px;
    font-size: clamp(0.78rem, 2.05vw, 0.92rem);
    line-height: 1.38;
  }

  #screen-game .code-panel-footer {
    display: flex;
    align-items: center;
    flex: 0 0 auto;
    min-height: 24px;
    margin: 5px 1px 0;
    padding: 0;
  }

  #screen-game .code-panel-footer label {
    margin: 0;
    color: #334155;
    font-size: 0.75rem;
    font-weight: 900;
    line-height: 1.1;
  }

  #screen-game .code-level-meta {
    font-size: 0.64rem;
  }

  #code-kids-translation-bubble {
    max-width: min(230px, 34vw) !important;
    font-size: 12px !important;
  }

  /* System keyboard fallback: side dock leaves the grid while Android IME is active. */
  body.ck-system-keyboard-mode #screen-game .game-grid {
    grid-template-columns: minmax(0, 0.88fr) minmax(0, 1.12fr);
    grid-template-areas: "preview code";
  }

  body.ck-system-keyboard-mode #code-kids-keyboard.ck-keyboard--system {
    position: fixed;
    right: max(6px, env(safe-area-inset-right));
    bottom: max(6px, env(safe-area-inset-bottom));
    left: auto;
    z-index: 95;
    width: auto;
    height: auto;
  }

  body.ck-keyboard-dragging #screen-game .preview-panel,
  body.ck-keyboard-dragging #screen-game .code-panel {
    pointer-events: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  #code-kids-keyboard {
    transition: none !important;
  }
}

/* ==========================================================
   Code Kids — 7.1C.3A
   Landscape result sizing + native caret refinement
   ========================================================== */

@media (orientation: landscape) and (max-height: 520px) and (max-width: 900px) {
  /* Result screen: compact dialog instead of portrait-sized poster. */
  #screen-result.active {
    height: 100%;
    min-height: 0;
    padding: 4px;
    overflow: hidden;
  }

  #screen-result .result-panel {
    width: min(88vw, 500px);
    max-width: 500px;
    max-height: calc(var(--ck-visual-height, 100dvh) - 10px);
    padding: 12px 18px;
    overflow-y: auto;
    border-radius: 18px;
  }

  #screen-result .result-icon {
    margin-bottom: 2px;
    font-size: clamp(2rem, 8vh, 2.7rem);
    line-height: 1;
  }

  #screen-result #result-title {
    margin-bottom: 2px;
    font-size: clamp(1.22rem, 5.8vh, 1.55rem);
    line-height: 1.08;
  }

  #screen-result #result-message {
    margin-bottom: 4px;
    font-size: clamp(0.78rem, 3.7vh, 0.96rem);
    line-height: 1.2;
  }

  #screen-result .stars {
    margin: 6px 0;
    font-size: clamp(1.35rem, 6.4vh, 1.75rem);
    line-height: 1;
    letter-spacing: 0.08em;
  }

  #screen-result .result-details {
    margin-bottom: 5px;
    font-size: clamp(0.76rem, 3.6vh, 0.92rem);
    line-height: 1.3;
  }

  #screen-result .btn-large {
    min-height: 42px;
    margin-top: 5px;
    padding: 7px 12px;
    border-radius: 12px;
    font-size: clamp(0.76rem, 3.4vh, 0.9rem);
  }

  /* Explicitly restore the familiar blinking text cursor. */
  body.ck-custom-keyboard-active #code-input {
    caret-color: #f8fafc !important;
    cursor: text;
  }
}
