/**
 * Code Kids — Mobile UI 7.1A + 7.1B
 * Scope: mobile contract + portrait layout.
 *
 * This file intentionally overlays the frozen Core V1 style.css.
 * Core/game logic is not changed here.
 */

:root {
  --ck-mobile-gutter: 12px;
  --ck-mobile-gap: 10px;
  --ck-mobile-radius: 18px;
  --ck-touch-min: 52px;
  --ck-visual-height: 100dvh;
}

/* General touch/accessibility improvements that are safe on all widths. */
.btn,
.icon-btn {
  touch-action: manipulation;
}

.btn:focus-visible,
.icon-btn:focus-visible,
#code-input:focus-visible {
  outline: 3px solid rgba(79, 70, 229, 0.28);
  outline-offset: 3px;
}

#code-input {
  caret-color: #ffffff;
}

@media (max-width: 699px) {
  html {
    min-height: 100%;
    background: #f3f4f6;
    overscroll-behavior-y: contain;
  }

  body {
    min-height: var(--ck-visual-height, 100dvh);
    overscroll-behavior-y: contain;
    background:
      radial-gradient(circle at 50% -8%, #ffffff 0, #f5f6fa 42%, #eef0f4 100%);
  }

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

  .screen {
    min-height: calc(var(--ck-visual-height, 100dvh) - 18px);
  }

  .panel {
    border-radius: var(--ck-mobile-radius);
    box-shadow: 0 10px 28px rgba(15, 23, 42, 0.065);
  }

  /* Menu / pause / result stay centered but use less dead space. */
  .menu-panel {
    width: 100%;
    max-width: 520px;
    padding: 24px 18px;
    border-radius: 22px;
  }

  h1 {
    font-size: clamp(2.55rem, 13vw, 4rem);
  }

  .lead {
    margin-bottom: 22px;
  }

  .btn {
    min-height: var(--ck-touch-min);
    padding-inline: 14px;
  }

  .btn-large {
    min-height: 58px;
  }

  .icon-btn {
    width: var(--ck-touch-min);
    height: var(--ck-touch-min);
    flex: 0 0 var(--ck-touch-min);
  }

  /* Game top bar: compact and stable under safe-area. */
  #screen-game.active {
    min-height: var(--ck-visual-height, 100dvh);
  }

  .game-header {
    position: sticky;
    top: max(0px, env(safe-area-inset-top));
    z-index: 30;
    margin: 0 -4px 10px;
    padding: 8px 8px 9px;
    border: 1px solid rgba(229, 231, 235, 0.78);
    border-radius: 18px;
    background: rgba(250, 251, 253, 0.92);
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.055);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }

  .game-header .eyebrow {
    margin-bottom: 3px;
    font-size: 0.7rem;
    letter-spacing: 0.07em;
  }

  .game-header h2 {
    max-width: calc(100vw - 105px);
    overflow: hidden;
    font-size: 1.12rem;
    line-height: 1.2;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .game-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--ck-mobile-gap);
    align-content: start;
    flex: 0 0 auto;
  }

  .task-panel,
  .preview-panel,
  .code-panel {
    min-width: 0;
    padding: 14px;
  }

  .task-panel h3,
  .preview-panel h3 {
    margin-bottom: 7px;
    font-size: 0.83rem;
    letter-spacing: 0.045em;
    text-transform: uppercase;
    color: #667085;
  }

  .task-panel p {
    margin-bottom: 11px;
    color: #344054;
    font-size: 0.96rem;
    line-height: 1.42;
  }

  #btn-hint {
    width: 100%;
    min-height: 48px;
  }

  .hint {
    margin-top: 10px;
    padding: 11px 12px;
    border-radius: 13px;
    font-size: 0.9rem;
    line-height: 1.4;
  }

  /* Preview stays visible, but is intentionally shallower on phones. */
  .preview-stage {
    min-height: clamp(126px, 24vh, 190px);
    max-height: 210px;
    overflow: hidden;
    border-radius: 16px;
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  }

  .preview-object {
    max-width: min(250px, 72vw);
    max-height: min(250px, 26vh);
  }

  .feedback {
    min-height: 20px;
    margin: 9px 2px 0;
    font-size: 0.9rem;
    line-height: 1.35;
  }

  .code-panel {
    padding-bottom: 10px;
  }

  .code-panel label {
    margin-bottom: 7px;
    font-size: 0.92rem;
  }

  #code-input {
    min-height: clamp(112px, 20vh, 160px);
    max-height: 230px;
    resize: vertical;
    padding: 13px 14px;
    border-radius: 14px;
    font-size: 16px;
    line-height: 1.52;
    tab-size: 2;
  }

  /* Important controls remain reachable near the thumb at the bottom. */
  .code-actions {
    position: sticky;
    bottom: max(4px, env(safe-area-inset-bottom));
    z-index: 28;
    grid-template-columns: minmax(98px, 0.72fr) minmax(0, 1.28fr);
    gap: 8px;
    margin: 9px -3px -3px;
    padding: 7px;
    border: 1px solid rgba(224, 226, 231, 0.86);
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 10px 26px rgba(15, 23, 42, 0.10);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }

  .code-actions .btn {
    min-width: 0;
    min-height: var(--ck-touch-min);
    padding: 10px 11px;
    white-space: nowrap;
  }

  #btn-run {
    box-shadow: 0 8px 18px rgba(79, 70, 229, 0.18);
  }

  /* Translation bubble is created by renderer.js; normalize it on phones. */
  #code-kids-translation-bubble {
    max-width: min(280px, calc(100vw - 24px)) !important;
    font-size: 14px !important;
  }

  /* Keyboard-aware compact mode. mobile-ui.js toggles this class. */
  body.ck-keyboard-open .app-shell {
    padding-top: max(4px, env(safe-area-inset-top));
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }

  body.ck-keyboard-open .game-header {
    position: static;
    margin-bottom: 7px;
    padding-block: 6px;
  }

  body.ck-keyboard-open .game-header .eyebrow {
    display: none;
  }

  body.ck-keyboard-open .task-panel {
    padding-block: 10px;
  }

  body.ck-keyboard-open .task-panel h3 {
    display: none;
  }

  body.ck-keyboard-open .task-panel p {
    margin-bottom: 8px;
    font-size: 0.9rem;
    line-height: 1.32;
  }

  body.ck-keyboard-open #btn-hint {
    min-height: 44px;
  }

  body.ck-keyboard-open .preview-panel {
    padding-block: 9px;
  }

  body.ck-keyboard-open .preview-panel h3 {
    display: none;
  }

  body.ck-keyboard-open .preview-stage {
    min-height: 82px;
    height: 15vh;
    max-height: 108px;
  }

  body.ck-keyboard-open .feedback {
    margin-top: 6px;
    font-size: 0.84rem;
  }

  body.ck-keyboard-open .code-panel {
    padding-top: 10px;
  }

  body.ck-keyboard-open #code-input {
    min-height: 96px;
    height: min(21vh, 132px);
    max-height: 136px;
  }

  /* Short phones: avoid preview swallowing the first viewport. */
  @media (max-height: 700px) {
    .task-panel,
    .preview-panel,
    .code-panel {
      padding: 12px;
    }

    .preview-stage {
      min-height: 110px;
      height: 20vh;
    }

    #code-input {
      min-height: 104px;
    }
  }
}

/* 7.1B is portrait-first. Landscape remains on the existing responsive rules
   until 7.1C, but prevent accidental clipping at phone widths. */
@media (max-height: 520px) and (orientation: landscape) {
  .app-shell {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .screen {
    min-height: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  .game-header,
  .code-actions {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

/* ==========================================================
   Code Kids — 7.1B.1 Portrait tuning
   Device baseline observed: 360 × 699 CSS px (Galaxy S21 FE 5G)
   Rules remain viewport-based, not device-model-based.
   ========================================================== */

@media (max-width: 699px) and (orientation: portrait) {
  /* Top level header remains, but becomes lighter after moving Pause away. */
  .game-header {
    padding: 7px 10px 8px;
    margin-bottom: 8px;
  }

  .game-header > div {
    min-width: 0;
    width: 100%;
  }

  .game-header .eyebrow {
    margin-bottom: 1px;
  }

  .game-header h2 {
    max-width: 100%;
    font-size: 1.06rem;
  }

  /* Slightly tighter content density without sacrificing touch comfort. */
  .game-grid {
    gap: 9px;
  }

  .task-panel,
  .preview-panel,
  .code-panel {
    padding: 12px;
  }

  .task-panel h3 {
    margin-bottom: 6px;
  }

  .task-panel p {
    margin-bottom: 9px;
    font-size: 0.94rem;
    line-height: 1.38;
  }

  #btn-hint {
    min-height: 46px;
  }

  /* The generic "Efekt" label is replaced with the actual level title. */
  .preview-level-heading {
    margin-bottom: 8px;
    overflow: hidden;
    color: #344054;
    font-size: 0.96rem;
    font-weight: 800;
    letter-spacing: 0;
    line-height: 1.2;
    text-overflow: ellipsis;
    text-transform: none;
    white-space: nowrap;
  }

  .preview-stage {
    min-height: clamp(112px, 20vh, 156px);
    height: clamp(112px, 20vh, 156px);
    max-height: 156px;
  }

  .preview-object {
    max-width: min(220px, 66vw);
    max-height: min(220px, 22vh);
  }

  .feedback {
    margin-top: 7px;
    font-size: 0.87rem;
  }

  /* New code header: "Kod · Poziom X" + Pause on the right. */
  .code-panel {
    padding-bottom: 11px;
  }

  .code-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: var(--ck-touch-min);
    margin-bottom: 7px;
  }

  .code-panel-header label {
    min-width: 0;
    margin: 0;
    color: #1f2937;
    font-size: 1rem;
    font-weight: 800;
    line-height: 1.2;
  }

  .code-level-separator {
    margin-inline: 3px;
    color: #98a2b3;
    font-weight: 700;
  }

  .code-level-meta {
    color: #667085;
    font-size: 0.83rem;
    font-weight: 800;
    letter-spacing: 0.035em;
    text-transform: uppercase;
    white-space: nowrap;
  }

  .mobile-pause-slot {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex: 0 0 auto;
    min-width: var(--ck-touch-min);
    min-height: var(--ck-touch-min);
  }

  .mobile-pause-slot:empty {
    display: none;
  }

  .mobile-pause-slot #btn-pause {
    width: var(--ck-touch-min);
    height: var(--ck-touch-min);
    border-radius: 14px;
    box-shadow: none;
  }

  #code-input {
    min-height: clamp(122px, 21vh, 164px);
    max-height: 230px;
  }

  /*
   * Closed keyboard: action row participates in normal document flow.
   * This prevents the Reset/Run bar from visually covering the editor.
   */
  body:not(.ck-keyboard-open) .code-actions {
    position: static;
    margin: 9px 0 0;
  }

  /*
   * Open keyboard: keep controls reachable near the lower visual viewport.
   */
  body.ck-keyboard-open .code-actions {
    position: sticky;
    bottom: max(4px, env(safe-area-inset-bottom));
  }

  body.ck-keyboard-open .code-panel-header {
    min-height: 46px;
    margin-bottom: 5px;
  }

  body.ck-keyboard-open .mobile-pause-slot #btn-pause {
    width: 46px;
    height: 46px;
  }

  body.ck-keyboard-open .preview-level-heading {
    display: none;
  }

  body.ck-keyboard-open .preview-stage {
    min-height: 78px;
    height: 13vh;
    max-height: 100px;
  }

  body.ck-keyboard-open #code-input {
    min-height: 102px;
    height: min(22vh, 136px);
    max-height: 140px;
  }

  @media (max-height: 720px) {
    .game-header {
      padding-block: 6px;
    }

    .task-panel,
    .preview-panel,
    .code-panel {
      padding: 11px;
    }

    .preview-stage {
      min-height: 108px;
      height: 18vh;
      max-height: 136px;
    }

    #code-input {
      min-height: 118px;
    }
  }
}

/* Desktop / current landscape keep Pause in the original top header. */
@media (min-width: 700px), (orientation: landscape) {
  .mobile-pause-slot {
    display: none;
  }
}

/* ==========================================================
   Code Kids — 7.1B.2
   Swap in Code panel:
   TOP    = Reset + Run + Pause
   CENTER = editor
   BOTTOM = Kod · Poziom X
   ========================================================== */

@media (max-width: 699px) and (orientation: portrait) {
  .code-panel-topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
  }

  .code-panel-topbar .code-actions {
    position: static !important;
    display: grid;
    grid-template-columns: minmax(88px, 0.72fr) minmax(0, 1.28fr);
    gap: 8px;
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }

  .code-panel-topbar .code-actions .btn {
    min-width: 0;
    min-height: var(--ck-touch-min);
    padding-inline: 9px;
    white-space: nowrap;
  }

  .code-panel-topbar .mobile-pause-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 var(--ck-touch-min);
    min-width: var(--ck-touch-min);
    min-height: var(--ck-touch-min);
  }

  .code-panel-footer {
    display: flex;
    align-items: center;
    min-height: 34px;
    margin-top: 8px;
    padding-inline: 2px;
  }

  .code-panel-footer label {
    margin: 0;
    color: #1f2937;
    font-size: 1rem;
    font-weight: 800;
    line-height: 1.2;
  }

  /* Buttons are now above the editor, so sticky bottom behavior is disabled. */
  body:not(.ck-keyboard-open) .code-actions,
  body.ck-keyboard-open .code-actions {
    position: static;
    bottom: auto;
  }

  body.ck-keyboard-open .code-panel-topbar {
    gap: 6px;
    margin-bottom: 6px;
  }

  body.ck-keyboard-open .code-panel-topbar .code-actions .btn {
    min-height: 46px;
    padding-inline: 7px;
    font-size: 0.9rem;
  }

  body.ck-keyboard-open .code-panel-topbar .mobile-pause-slot {
    flex-basis: 46px;
    min-width: 46px;
    min-height: 46px;
  }

  body.ck-keyboard-open .code-panel-footer {
    min-height: 30px;
    margin-top: 5px;
  }

  body.ck-keyboard-open .code-panel-footer label {
    font-size: 0.9rem;
  }
}

/* ==========================================================
   Code Kids — 7.1B.3
   Extra breathing room between top controls and code editor.
   ========================================================== */

@media (max-width: 699px) and (orientation: portrait) {
  .code-panel-topbar {
    margin-bottom: 14px;
  }

  body.ck-keyboard-open .code-panel-topbar {
    margin-bottom: 10px;
  }
}

/* ==========================================================
   Code Kids — 7.1B.4
   Keyboard dock: Code panel sits close to the keyboard.
   ========================================================== */

@media (max-width: 699px) and (orientation: portrait) {
  body.ck-keyboard-open .code-panel {
    margin-bottom: 2px;
    scroll-margin-bottom: max(4px, env(safe-area-inset-bottom));
  }

  body.ck-keyboard-open #code-input {
    scroll-margin-bottom: 4px;
  }
}

/* ==========================================================
   Code Kids — 7.1C.3B Portrait cleanup
   Remove duplicated level header and text-heavy task description.
   Hint remains available as a compact control.
   ========================================================== */

@media (orientation: portrait) and (max-width: 699px) {
  #screen-game .game-header {
    display: none !important;
  }

  #screen-game .task-panel {
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
  }

  #screen-game .task-panel h3,
  #screen-game #task-text {
    display: none !important;
  }

  #screen-game .task-panel #btn-hint {
    width: 100%;
    min-height: 44px;
    margin: 0;
    border-radius: 14px;
  }

  #screen-game .task-panel .hint {
    margin: 6px 0 0;
  }

  #screen-game .game-grid {
    gap: 9px;
  }

  body.ck-custom-keyboard-active #screen-game #code-input {
    min-height: 112px;
  }
}
