:root {
      --ink: #10233d;
      --paper: #eef5e8;
      --panel: #f7f4d8;
      --panel-dark: #223b57;
      --line: #10233d;
      --accent: #11a36a;
      --warning: #ffb000;
      --danger: #e84a5f;
      --water: #2b83ba;
      --shadow: #0b1728;
    }
    * { box-sizing: border-box; }

    html, body {
      margin: 0;
      min-height: 100%;
      background:
        linear-gradient(135deg, rgba(255,255,255,.04) 25%, transparent 25%) 0 0 / 16px 16px,
        linear-gradient(315deg, rgba(255,255,255,.04) 25%, transparent 25%) 0 0 / 16px 16px,
        #081522;
      color: #fff;
      font-family: "Courier New", Consolas, monospace;
      image-rendering: pixelated;
    }

    body {
      display: flex;
      justify-content: center;
      padding: 18px;
    }

    .app {
      width: min(1500px, 100%);
      display: grid;
      grid-template-columns: minmax(0, 1fr) 330px;
      gap: 14px;
    }

    .game-shell,
    .side-panel,
    .control-bar,
    .mobile-controls {
      border: 4px solid var(--line);
      box-shadow: 6px 6px 0 var(--shadow);
      background: var(--panel);
      color: var(--ink);
    }

    .brand-bar {
      border: 4px solid var(--line);
      border-bottom: 0;
      background: var(--panel-dark);
      color: #fff;
      padding: 10px 14px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 12px;
    }

    .brand {
      font-weight: 900;
      letter-spacing: .5px;
      text-transform: uppercase;
      font-size: clamp(15px, 2vw, 22px);
    }

    .brand small {
      display: block;
      margin-top: 3px;
      color: #9fe6ca;
      font-size: 10px;
      letter-spacing: 1px;
    }

    .status-chip {
      border: 3px solid #fff;
      padding: 6px 9px;
      font-size: 11px;
      white-space: nowrap;
      background: #163149;
    }

    .canvas-wrap {
      position: relative;
      overflow: hidden;
      background: #0a2031;
    }

    canvas {
      display: block;
      width: 100%;
      height: auto;
      max-height: calc(100vh - 190px);
      background: #86c56c;
      image-rendering: pixelated;
      image-rendering: crisp-edges;
      cursor: crosshair;
    }

    .canvas-message {
      position: absolute;
      left: 50%;
      bottom: 15px;
      transform: translateX(-50%);
      background: rgba(12, 27, 45, .94);
      color: #fff;
      border: 3px solid #fff;
      padding: 8px 12px;
      width: min(680px, calc(100% - 24px));
      text-align: center;
      font-size: 12px;
      pointer-events: none;
    }

    .control-bar {
      margin-top: 12px;
      padding: 10px;
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      align-items: center;
    }

    button, select {
      font: inherit;
      border: 3px solid var(--line);
      background: #fffef2;
      color: var(--ink);
      padding: 8px 10px;
      font-weight: 800;
      box-shadow: 3px 3px 0 var(--shadow);
      cursor: pointer;
    }

    button:hover, select:hover { transform: translate(-1px, -1px); }
    button:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--shadow); }
    button.primary { background: #a8f0c8; }
    button.warn { background: #ffd36a; }
    button.danger { background: #ff9a9f; }

    .control-bar .spacer { flex: 1; }

    .key-hint {
      font-size: 11px;
      line-height: 1.4;
      color: #3f5368;
    }

    .side-panel {
      min-width: 0;
      padding: 12px;
      align-self: start;
      position: sticky;
      top: 12px;
      max-height: calc(100vh - 24px);
      overflow: auto;
    }

    .panel-title {
      margin: 0 0 10px;
      border-bottom: 3px solid var(--line);
      padding-bottom: 7px;
      font-size: 17px;
      text-transform: uppercase;
    }

    .section {
      border: 3px solid var(--line);
      background: #fffef2;
      margin-bottom: 10px;
      padding: 9px;
    }

    .section h3 {
      font-size: 12px;
      margin: 0 0 8px;
      text-transform: uppercase;
      color: #36536c;
    }

    .metric-grid {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 6px;
    }

    .metric {
      border: 2px solid var(--line);
      background: #e8f1df;
      padding: 7px;
      min-width: 0;
    }

    .metric label {
      display: block;
      font-size: 9px;
      color: #50657a;
      text-transform: uppercase;
      margin-bottom: 3px;
    }

    .metric strong {
      display: block;
      font-size: 15px;
      overflow-wrap: anywhere;
    }

    .bar {
      height: 13px;
      border: 2px solid var(--line);
      background: #d7dfd1;
      margin-top: 4px;
      overflow: hidden;
    }

    .bar > span {
      display: block;
      height: 100%;
      width: 50%;
      background: var(--accent);
      transition: width .25s linear;
    }

    .mission {
      font-size: 11px;
      line-height: 1.45;
    }

    .action-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 6px;
    }

    .action-grid button {
      padding: 8px 5px;
      font-size: 10px;
      min-height: 50px;
    }

    .selected-pole {
      min-height: 94px;
      font-size: 11px;
      line-height: 1.4;
    }

    .log {
      height: 145px;
      overflow-y: auto;
      font-size: 10px;
      line-height: 1.45;
      background: #0e2135;
      color: #dcf8ec;
      border: 3px solid var(--line);
      padding: 8px;
    }

    .log div { margin-bottom: 5px; }
    .log .danger-text { color: #ff9a9f; }
    .log .good-text { color: #a8f0c8; }
    .log .info-text { color: #9fd8ff; }

    .legend {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 5px;
      font-size: 9px;
    }

    .legend span { display: flex; align-items: center; gap: 5px; }
    .swatch { width: 12px; height: 12px; border: 2px solid var(--line); display: inline-block; }

    .mobile-controls {
      display: none;
      margin-top: 12px;
      padding: 10px;
      grid-template-columns: 120px 1fr;
      gap: 12px;
    }

    .dpad {
      display: grid;
      grid-template-columns: repeat(3, 38px);
      grid-template-rows: repeat(3, 38px);
      gap: 3px;
      justify-content: center;
    }

    .dpad button, .mobile-action button {
      padding: 0;
      min-width: 0;
      min-height: 0;
      touch-action: none;
    }

    .mobile-action {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 6px;
    }

    .mobile-action button { padding: 8px; }

    .footer-note {
      color: #90a7bb;
      font-size: 10px;
      margin-top: 12px;
      line-height: 1.5;
    }

    @media (max-width: 1060px) {
      .app { grid-template-columns: 1fr; }
      .side-panel { position: static; max-height: none; }
      canvas { max-height: none; }
      .mobile-controls { display: grid; }
    }

    @media (max-width: 620px) {
      body { padding: 8px; }
      .brand-bar { align-items: flex-start; flex-direction: column; }
      .status-chip { width: 100%; text-align: center; }
      .control-bar { align-items: stretch; }
      .control-bar > * { flex: 1 1 45%; }
      .control-bar .spacer { display: none; }
      .key-hint { flex-basis: 100%; }
      .mobile-controls { grid-template-columns: 1fr; }
      .metric-grid { grid-template-columns: 1fr 1fr; }
    }


    /* Mobile gameplay interface */
    .mobile-controls {
      display: none;
    }

    .mobile-control-title {
      margin-bottom: 4px;
      color: #d8f1ff;
      font-size: 9px;
      font-weight: 900;
      letter-spacing: 1px;
      text-align: center;
    }

    .dpad-centre {
      display: grid;
      place-items: center;
      color: rgba(255,255,255,.42);
      font-size: 18px;
      font-weight: 900;
    }

    .interact-button {
      display: flex;
      min-height: 48px;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      background: #a8f0c8;
      line-height: 1.05;
    }

    .interact-button strong {
      font-size: 12px;
    }

    .interact-button small {
      margin-top: 3px;
      color: #36536c;
      font-size: 8px;
    }

    @media (max-width: 760px),
      (min-width: 761px) and (max-width: 1060px),
      (hover: none) and (pointer: coarse) and (max-width: 1440px),
      (min-width: 761px) and (max-width: 1440px) and (max-height: 1100px) and (min-aspect-ratio: 13 / 10) and (max-aspect-ratio: 33 / 20) {
      html {
        overscroll-behavior: none;
      }

      body {
        padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
      }

      .app {
        width: 100%;
        gap: 8px;
      }

      .brand-bar {
        padding: 8px 9px;
      }

      .brand {
        font-size: 15px;
      }

      .brand small {
        font-size: 8px;
      }

      .game-shell,
      .side-panel,
      .control-bar {
        border-width: 3px;
        box-shadow: 4px 4px 0 var(--shadow);
      }

      .canvas-wrap {
        width: 100%;
        aspect-ratio: auto;
      }

      canvas {
        width: 100%;
        height: auto;
        aspect-ratio: 16 / 9;
        max-height: none;
        object-fit: contain;
      }

      .canvas-message {
        position: static;
        width: 100%;
        transform: none;
        border-width: 2px;
        padding: 5px 7px;
        font-size: 9px;
        line-height: 1.25;
      }

      .control-bar {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 7px;
        margin-top: 8px;
        padding: 8px;
      }

      .control-bar label,
      .control-bar select,
      .control-bar .key-hint {
        grid-column: 1 / -1;
      }

      .control-bar select,
      .control-bar button {
        width: 100%;
        min-height: 42px;
        padding: 7px;
        font-size: 11px;
      }

      .control-bar .spacer {
        display: none;
      }

      .key-hint {
        display: none;
      }

      .side-panel {
        padding: 8px;
      }

      .panel-title {
        font-size: 15px;
      }

      .section {
        margin-bottom: 8px;
        padding: 8px;
      }

      .metric-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
      }

      .action-grid button {
        min-height: 44px;
        font-size: 9px;
      }

      .mobile-controls {
        position: static;
        display: grid;
        grid-template-columns: 138px minmax(0, 1fr);
        gap: 8px;
        width: 100%;
        max-width: none;
        margin: 8px 0 0;
        padding: 7px;
        border: 3px solid #d8f1ff;
        border-radius: 0;
        background: rgba(14, 33, 53, .92);
        box-shadow: 4px 4px 0 rgba(4, 11, 18, .82);
        color: #fff;
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
        touch-action: none;
        user-select: none;
        -webkit-user-select: none;
      }

      .mobile-move-group,
      .mobile-interaction-group {
        min-width: 0;
      }

      .dpad {
        display: grid;
        grid-template-columns: repeat(3, 42px);
        grid-template-rows: repeat(3, 42px);
        gap: 3px;
        justify-content: center;
      }

      .dpad button,
      .mobile-action button,
      .interact-button {
        margin: 0;
        border: 2px solid #d8f1ff;
        box-shadow: 2px 2px 0 #050d16;
        touch-action: none;
        -webkit-tap-highlight-color: transparent;
      }

      .dpad button {
        width: 42px;
        height: 42px;
        padding: 0;
        background: #fff7e2;
        font-size: 17px;
      }

      .dpad button:active,
      .dpad button.is-pressed,
      .mobile-action button:active,
      .interact-button:active {
        transform: translate(1px, 1px);
        background: #ffd36a;
        box-shadow: 1px 1px 0 #050d16;
      }

      .mobile-interaction-group {
        display: grid;
        grid-template-rows: auto 1fr;
        gap: 6px;
      }

      .mobile-action {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 5px;
      }

      .mobile-action button {
        min-height: 46px;
        padding: 5px 4px;
        background: #fff7e2;
        font-size: 9px;
        line-height: 1.2;
        white-space: normal;
      }
    }

    @media (max-width: 410px) {
      body {
        padding-right: 4px;
        padding-left: 4px;
        padding-bottom: calc(4px + env(safe-area-inset-bottom));
      }

      .mobile-controls {
        grid-template-columns: 126px minmax(0, 1fr);
        gap: 6px;
        padding: 6px;
      }

      .dpad {
        grid-template-columns: repeat(3, 38px);
        grid-template-rows: repeat(3, 38px);
      }

      .dpad button {
        width: 38px;
        height: 38px;
        font-size: 15px;
      }

      .interact-button {
        min-height: 42px;
      }

      .interact-button strong {
        font-size: 11px;
      }

      .mobile-action button {
        min-height: 52px;
        font-size: 8px;
      }

      .status-chip {
        padding: 5px 7px;
        font-size: 9px;
      }
    }

    /* Desktop-style composition for phones and smaller tablets in landscape. */
    @media (min-width: 560px) and (max-width: 1060px) and (orientation: landscape) {
      body {
        padding: 6px;
      }

      .app {
        grid-template-columns: minmax(0, 1fr) clamp(230px, 32vw, 330px);
        gap: 8px;
        align-items: start;
      }

      .brand-bar {
        flex-direction: row;
        align-items: center;
      }

      .side-panel {
        position: sticky;
        top: 6px;
        max-height: calc(100vh - 12px);
        overflow-y: auto;
      }

      .mobile-controls {
        grid-template-columns: 138px minmax(0, 1fr);
      }

      .dpad {
        grid-template-columns: repeat(3, 42px);
        grid-template-rows: repeat(3, 42px);
      }

      .dpad button {
        width: 42px;
        height: 42px;
      }

      .mobile-action button {
        min-height: 46px;
        font-size: 9px;
      }
    }

    /* In-game touch overlay for phones held horizontally. */
    @media (min-width: 560px) and (max-width: 1060px) and (max-height: 520px) and (orientation: landscape) {
      .mobile-controls {
        position: absolute;
        z-index: 5;
        inset: 0;
        display: flex;
        width: 100%;
        height: auto;
        margin: 0;
        padding: 12px;
        align-items: flex-end;
        justify-content: space-between;
        border: 0;
        background: transparent;
        box-shadow: none;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        pointer-events: none;
      }

      .mobile-move-group,
      .mobile-interaction-group {
        pointer-events: auto;
        opacity: .38;
        transition: opacity 140ms ease;
      }

      .mobile-move-group:hover,
      .mobile-move-group:focus-within,
      .mobile-interaction-group:hover,
      .mobile-interaction-group:focus-within {
        opacity: .92;
      }

      .mobile-control-title,
      .mobile-action,
      .interact-button small {
        display: none;
      }

      .mobile-interaction-group {
        display: block;
      }

      .dpad button,
      .interact-button {
        background: rgba(255, 247, 226, .82);
      }

      .dpad button:active,
      .dpad button.is-pressed,
      .interact-button:active {
        opacity: 1;
      }

      .interact-button {
        min-width: 112px;
        min-height: 52px;
        padding: 8px 12px;
      }

      .canvas-message {
        position: absolute;
        z-index: 6;
        right: 7px;
        bottom: 7px;
        left: 7px;
        width: auto;
      }

      .mobile-controls {
        padding-bottom: 42px;
      }
    }
