/* Mobile application shell: optimized for iPhone/PWA without changing desktop. */
button, a, input, select, textarea { -webkit-tap-highlight-color: transparent; }

@media (max-width: 720px) {
  :root { --mobile-gap: 10px; }
  html { min-height: 100%; background: var(--bg); }
  body {
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    padding-bottom: calc(76px + env(safe-area-inset-bottom));
    overscroll-behavior-y: none;
  }
  .shell {
    width: 100%;
    padding: calc(8px + env(safe-area-inset-top)) 0 18px;
  }
  .glass { box-shadow: inset 0 1px rgba(255,255,255,.035), 0 12px 34px rgba(0,0,0,.25); }

  /* Compact status header + native-like bottom navigation. */
  .topbar {
    min-height: 62px;
    margin: 0 var(--mobile-gap) var(--mobile-gap);
    padding: 9px 12px;
    border-radius: 16px;
    grid-template-columns: 1fr auto;
    gap: 8px;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .brand { gap: 9px; }
  .sigil { width: 37px; height: 37px; font-size: 19px; }
  .brand b { font-size: 17px; letter-spacing: .18em; }
  .brand small { display: none; }
  .system { grid-column: 2; grid-row: 1; gap: 7px; }
  .system b { max-width: 126px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; }
  .system small { font-size: 10px; }
  .pulse { width: 8px; height: 8px; }
  .logout { display: none; }
  .view-tabs {
    position: fixed;
    z-index: 100;
    left: 0;
    right: 0;
    bottom: 0;
    grid-column: auto;
    grid-row: auto;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0;
    padding: 5px 8px calc(5px + env(safe-area-inset-bottom));
    border: 0;
    border-top: 1px solid rgba(88,230,255,.22);
    background: rgba(3,9,18,.94);
    box-shadow: 0 -12px 35px rgba(0,0,0,.45);
    backdrop-filter: blur(22px);
    -webkit-backdrop-filter: blur(22px);
  }
  .view-tab {
    min-width: 0;
    min-height: 58px;
    padding: 5px 2px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    align-content: center;
    gap: 2px;
    font-size: 0;
  }
  .view-tab::before { color: var(--muted); font: 20px/1 var(--mono); }
  .view-tab::after { color: var(--muted); font: 700 9px/1.2 var(--mono); letter-spacing: .04em; }
  .view-tab:nth-child(1)::before { content: "⌂"; }.view-tab:nth-child(1)::after { content: "ОБЗОР"; }
  .view-tab:nth-child(2)::before { content: "▥"; }.view-tab:nth-child(2)::after { content: "СТАТИСТИКА"; }
  .view-tab:nth-child(3)::before { content: "⚙"; }.view-tab:nth-child(3)::after { content: "НАСТРОЙКИ"; }
  .view-tab:nth-child(4)::before { content: "≡"; }.view-tab:nth-child(4)::after { content: "ЛОГИ"; }
  .view-tab.active { box-shadow: none; background: rgba(88,230,255,.1); }
  .view-tab.active::before,.view-tab.active::after { color: var(--cyan); }

  /* Overview density and content priority. */
  .resource-strip {
    grid-template-columns: repeat(2, minmax(0,1fr));
    gap: 8px;
    margin: 0 var(--mobile-gap) var(--mobile-gap);
  }
  .metric { min-height: 91px; padding: 13px 14px; border-radius: 14px; }
  .metric:nth-child(3) { grid-column: 1; grid-row: 2; }
  .metric:nth-child(4) { grid-column: 1/-1; grid-row: 3; }
  .metric:nth-child(5) { grid-column: 2; grid-row: 2; }
  .metric small { font-size: 9px; line-height: 1.35; letter-spacing: .07em; }
  .metric strong { margin-top: 7px; font-size: clamp(18px,5.2vw,23px); overflow-wrap: anywhere; }
  .metric-detail { font-size: 9px; }
  .layout { display: flex; flex-direction: column; gap: var(--mobile-gap); padding: 0 var(--mobile-gap); }
  .main-column,.side-column { display: contents; }
  .control { order: 1; }.dungeon { order: 2; }.intel { order: 3; }.main-chat { order: 4; }
  .health-card { order: 5; }.gold-card { order: 6; }.terminal { order: 7; }
  .dungeon,.terminal,.control,.intel,.gold-card,.chat-card,.stats-panel,.health-card { border-radius: 15px; padding: 15px; }
  .eyebrow { font-size: 10px; letter-spacing: .1em; }
  .control-primary button,.mode-grid button,.control .wide,.send-chat,.save-settings,.mini-action,.filter-actions button {
    min-height: 46px;
    touch-action: manipulation;
  }
  .control-primary { grid-template-columns: 1.45fr 1fr; }
  .mode-grid { grid-template-columns: 1fr 1fr; }
  .run-limit input { min-height: 44px; font-size: 16px; }
  .dungeon-head { align-items: flex-start; gap: 10px; }
  .dungeon h1 { margin-top: 13px; font-size: 23px; line-height: 1.14; }
  .dungeon p { flex-direction: column; gap: 3px; font-size: 13px; }
  .radar { width: 58px; height: 58px; font-size: 15px; }
  .participants { overflow: visible; }
  .participant-head { display: none; }
  .participant {
    min-width: 0;
    grid-template-columns: 1fr 1fr;
    gap: 7px 12px;
    padding: 11px 12px;
    border-left-width: 3px;
  }
  .participant b { grid-column: 1/-1; font-size: 14px; }
  .participant span { display: grid; gap: 2px; text-align: left; font-size: 12px; }
  .participant span:nth-child(2)::before { content: "ОБЩИЙ УРОН"; }
  .participant span:nth-child(3)::before { content: "ЗА ЭТАП"; }
  .participant span::before { color: #4f7288; font-size: 8px; letter-spacing: .08em; }
  .main-chat { min-height: 0; }
  .main-chat .chat-list { min-height: 0; max-height: 340px; }
  .chat-message { padding: 12px; border-radius: 10px; }
  .chat-form textarea,input,select { font-size: 16px; }
  .main-chat .chat-form { grid-template-columns: 1fr; }
  .main-chat .chat-form label { grid-column: auto; }
  .terminal { min-height: 350px; padding: 0; }
  .terminal-head { padding: 12px 14px; align-items: flex-start; flex-direction: column; }
  .chip { min-height: 38px; padding: 8px 11px; }
  .logs { height: 300px; padding: 12px; font-size: 11px; }
  .log { display: block; margin-bottom: 8px; overflow-wrap: anywhere; }
  .log time,.log b { margin-right: 7px; }
  .health-grid > div { grid-template-columns: 10px 1fr; }
  .health-grid b { grid-column: 2; }

  /* Statistics become readable cards instead of miniature desktop tables. */
  #statistics-view,#settings-view,#logs-view { padding: 0 var(--mobile-gap); }
  .stats-toolbar,.settings-toolbar,.settings-footer,.logs-toolbar { margin-top: 0; border-radius: 15px; padding: 15px; }
  .stats-grid { grid-template-columns: repeat(2,minmax(0,1fr)); gap: 8px; }
  .stat-card { min-height: 126px; padding: 14px; }
  .stat-card small { font-size: 9px; }
  .stat-card strong { margin: 9px 0 6px; font-size: 23px; }
  .stat-card p { font-size: 11px; }
  .stats-layout { gap: 10px; margin-top: 10px; }
  .stats-detail { margin-top: 10px; }
  .table-wrap { max-height: none; overflow: visible; }
  .stats-table,.stats-table tbody { display: block; width: 100%; }
  .stats-table thead { display: none; }
  .stats-table tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 7px 12px;
    margin-bottom: 8px;
    padding: 11px 12px;
    border: 1px solid rgba(91,213,255,.13);
    border-radius: 11px;
    background: rgba(2,9,18,.45);
  }
  .stats-table td,.stats-table .number {
    display: grid;
    gap: 2px;
    min-width: 0;
    padding: 0;
    border: 0;
    text-align: left;
    white-space: normal;
    overflow-wrap: anywhere;
    font-size: 12px;
  }
  .stats-table td::before { content: attr(data-label); color: #52758c; font: 8px var(--mono); letter-spacing: .08em; }
  .stats-table td[colspan] { grid-column: 1/-1; }
  .stats-table td[colspan]::before { content: none; }
  .detail-chart > div { grid-template-columns: 68px minmax(0,1fr) 68px; gap: 7px; }
  .detail-chart b { font-size: 10px; }

  /* Settings are finger-friendly and keep the primary action obvious. */
  .settings-toolbar,.settings-footer { align-items: stretch; flex-direction: column; }
  .settings-toolbar h1,.logs-toolbar h1 { font-size: 19px; }
  .settings-toolbar .save-settings,.settings-footer .save-settings,.logs-toolbar button { width: 100%; }
  .settings-grid { grid-template-columns: 1fr; gap: 10px; }
  .settings-schedule,.settings-filters { grid-column: auto; }
  .settings-card { padding: 15px; }
  .field-grid,.filter-group > div { grid-template-columns: 1fr; }
  .field input,.schedule-row input,.schedule-row select { min-height: 46px; font-size: 16px; }
  .switch { min-height: 44px; }
  .switch b { font-size: 14px; }
  .schedule-row { grid-template-columns: 105px minmax(0,1fr) 44px; }
  .schedule-row button { min-height: 46px; font-size: 20px; }
  .filter-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .filter-group summary { min-height: 48px; }
  .filter-group > div label { min-height: 42px; }
  .settings-footer { margin-bottom: 10px; }
  .pwa-grid { grid-template-columns: 1fr; margin-bottom: 10px; }
  .device-row { grid-template-columns: 1fr; border-radius: 10px; }
  .device-row > span { grid-column: auto; }
  .device-row button { width: 100%; min-height: 44px; }
  .log-files-panel { margin-top: 10px; padding: 15px; }
  .log-files-table td:last-child { text-align: left; }
  .download-log { display: block; min-height: 42px; padding: 11px; text-align: center; }
}

/* Login screen must survive the iOS keyboard and standalone safe areas. */
@media (max-width: 540px) {
  body:has(.login-shell) { padding: 0; overflow-y: auto; }
  .login-shell {
    min-height: 100dvh;
    padding: max(18px,env(safe-area-inset-top)) 14px max(18px,env(safe-area-inset-bottom));
    align-items: center;
  }
  .login-card { width: 100%; padding: 30px 22px 25px; border-radius: 22px; }
  .login-card .sigil { width: 62px; height: 62px; font-size: 29px; }
  .login-card .overline { margin-top: 20px; }
  .login-card h1 { font-size: 32px; }
  .login-card .subtitle { margin-bottom: 27px; }
  .input-wrap,.login-card form button { min-height: 52px; }
  .input-wrap input { font-size: 16px; }
  .remember { min-height: 44px; font-size: 12px; }
  .remember input { width: 20px; height: 20px; }
}

@media (max-width: 370px) {
  .resource-strip,.stats-grid { grid-template-columns: 1fr; }
  .metric:nth-child(4) { grid-column: auto; }
  .metric:nth-child(3),.metric:nth-child(4),.metric:nth-child(5) { grid-column: auto; grid-row: auto; }
  .view-tab::after { font-size: 8px; letter-spacing: 0; }
  .schedule-row { grid-template-columns: 94px minmax(0,1fr) 42px; }
}
