/* Keep the existing isometric projection; only its viewport changes size. */
.tool-list, .field-heading, .field-viewport, .field-stage { display: contents; }
.tool-icon { font-size: 23px; }
.tool-label, .market-shortcut, #field-zoom, .field-hint { display: none; }
.seed small { display: block; font-size: 9px; white-space: nowrap; }
button, .market-shortcut { touch-action: manipulation; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}

@media (max-width: 979px) {
  :root {
    --page-left: max(12px, env(safe-area-inset-left, 0px));
    --page-right: max(12px, env(safe-area-inset-right, 0px));
    --dock-space: calc(144px + env(safe-area-inset-bottom, 0px));
  }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; scroll-padding-bottom: calc(var(--dock-space) + 80px); }
  body { min-width: 0; width: 100%; background: #e6e7df; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
  button, input { font-family: inherit; }
  .game-shell {
    width: 100%; max-width: none; min-height: 100vh; min-height: 100dvh;
    padding: 0 var(--page-right) calc(var(--dock-space) + 100px) var(--page-left);
  }
  .topbar {
    height: auto; margin: 0 0 12px; padding: calc(10px + env(safe-area-inset-top, 0px)) 12px 10px;
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: 'farmer next' 'resources resources' 'account account';
    gap: 10px; border-radius: 0 0 18px 18px;
  }
  .farmer-card { grid-area: farmer; min-width: 0; gap: 8px; font-size: 15px; }
  .farmer-card > div:last-child { min-width: 0; }
  .farmer-card strong { max-width: none; }
  .farmer-card span { font-size: 12px; }
  .avatar { width: 40px; height: 40px; flex: 0 0 40px; font-size: 23px; }
  .sun-button {
    grid-area: next; width: auto; min-width: 78px; height: 46px; padding: 6px 9px;
    display: flex; align-items: center; justify-content: center; gap: 5px; border-radius: 13px; font-size: 17px;
  }
  .sun-button small { font-size: 12px; }
  .currencies { grid-area: resources; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .currencies div {
    min-width: 0; min-height: 34px; padding: 7px 4px; border-width: 1px;
    display: flex; align-items: center; justify-content: center; gap: 4px;
    border-radius: 10px; font-size: 12px; font-variant-numeric: tabular-nums; flex-wrap: wrap;
  }
  .currencies b { min-width: 0; overflow-wrap: anywhere; }
  .account-tools { grid-area: account; width: 100%; margin: 0; display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 6px; }
  #save-status { font-size: 11px; max-width: none; line-height: 1.4; align-self: center; }
  .account-tools button { min-height: 44px; min-width: 48px; padding: 8px 10px; font-size: 12px; }

  .game-area { display: flex; flex-direction: column; gap: 14px; width: 100%; height: auto; min-height: 0; padding: 0; }
  .farm { flex: none; width: 100%; height: auto; min-width: 0; border-width: 2px; border-radius: 18px; }
  .field-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px 0; }
  .field-label {
    position: static; transform: none; margin: 0; padding: 4px 8px; background: #f9f5d5;
    border: 1px solid #cad7a3; border-radius: 8px; box-shadow: none;
    font-size: 16px; line-height: 1.3; color: #385530;
  }
  #field-zoom {
    display: block; flex: 0 0 auto; min-height: 44px; max-width: 50%; padding: 8px 10px;
    border: 1px solid #6e9651; border-radius: 10px; background: #f5f8dd; color: #36552d;
    font: bold 12px 'Trebuchet MS', sans-serif; cursor: pointer;
  }
  #field-zoom[aria-pressed='true'] { background: #43673a; color: white; }
  .field-viewport {
    position: relative; display: block; width: 100%; min-height: 230px; max-height: 520px;
    aspect-ratio: 1.35; overflow: auto; touch-action: pan-x pan-y pinch-zoom;
    scrollbar-width: thin; scrollbar-color: #628d48 transparent;
  }
  .field-stage { position: relative; display: flow-root; width: 100%; min-height: 100%; padding: 0 12px 24px; }
  .farm[data-zoomed='true'] .field-stage { width: max(160%, 740px); }
  #plots { position: relative; top: auto; left: auto; width: 100%; max-width: none; margin: 12% 0 0; transform: none; }
  #plots .plot-cell .crop { font-size: clamp(18px, 5vw, 30px); }
  #plots .plot-cell .water { font-size: 12px; }
  #plots .plot:hover { filter: none; }
  #plots .plot:active, #plots .plot:focus-visible { filter: brightness(1.18); }
  .field-hint {
    display: block; margin: 0; padding: 9px 12px 12px; font-size: 12px; color: #36552d; line-height: 1.4;
    background: #f9f5d5; border-top: 1px solid #cad7a3;
  }

  .left-panel {
    position: fixed; z-index: 20; inset: auto 0 0; display: block; width: auto; height: auto; max-height: none;
    padding: 10px var(--page-right) calc(10px + env(safe-area-inset-bottom, 0px)) var(--page-left);
    border: 0; border-top: 1px solid #b9c6a2; border-radius: 18px 18px 0 0;
    background: #fbf8e8; box-shadow: 0 -4px 22px #33462224; overflow: visible;
  }
  .seed-list { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .seed { min-width: 0; width: 100%; min-height: 58px; height: auto; padding: 6px; gap: 5px; border-radius: 10px; }
  .seed .icon { font-size: 23px; flex-shrink: 0; }
  .seed b { font-size: 12px; }
  .seed small { display: block; margin-top: 3px; font-size: 10px; color: #68593c; white-space: nowrap; }
  .tool-list { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 8px; }
  .tool, .market-shortcut {
    display: flex; align-items: center; justify-content: center; gap: 4px; width: auto; min-width: 0; height: 48px;
    margin: 0; padding: 5px 3px; border: 1px solid #c7cca9; border-radius: 10px;
    background: #edf1dc; color: #466139; font: 12px 'Trebuchet MS', sans-serif; text-decoration: none;
  }
  .tool-icon { font-size: 20px; line-height: 1; }
  .tool-label { display: inline; }
  .tool.active { color: #fff; background: #4e703d; border-color: #4e703d; box-shadow: none; }
  .market-shortcut { color: #755124; background: #fbefd0; border-color: #d9c697; }

  .right-panel { position: static; flex: none; width: 100%; max-height: none; padding: 16px; border-radius: 16px; overflow: visible; scroll-margin-top: 12px; }
  .right-panel h2 { font-size: 18px; margin-bottom: 10px; }
  .right-panel p { display: block; font-size: 13px; }
  .right-panel .inventory { gap: 7px; min-height: 0; }
  .inv-row { padding: 10px 12px; font-size: 14px; border-radius: 9px; }
  .right-panel .primary { min-height: 48px; padding: 10px 12px; margin-top: 12px; font-size: 14px; }
  .text-button, .import-button { display: block; width: 100%; min-height: 44px; padding: 10px 8px; font-size: 12px; margin-top: 10px; }
  .message {
    position: fixed; z-index: 21; left: var(--page-left); right: var(--page-right); bottom: var(--dock-space);
    transform: none; width: auto; max-width: none; margin: 0; padding: 9px 12px;
    border-radius: 10px; font-size: 12px; line-height: 1.4; box-shadow: 0 2px 8px #2d422622;
    overflow-wrap: anywhere; pointer-events: none; opacity: 0; transition: opacity 160ms ease;
  }
  .message.is-visible { opacity: 1; }
  .auth-screen {
    min-height: 100vh; min-height: 100svh;
    padding: max(18px, env(safe-area-inset-top, 0px)) var(--page-right) max(18px, env(safe-area-inset-bottom, 0px)) var(--page-left);
  }
  .auth-card { width: min(100%, 440px); padding: 24px; }
  .auth-card input { font-size: 16px; min-height: 48px; }
  .auth-switch, .auth-primary, .file-notice button { min-height: 44px; }
}

@media (max-width: 360px) {
  .topbar { padding-left: 9px; padding-right: 9px; }
  .tool, .market-shortcut { flex-direction: column; gap: 3px; font-size: 11px; }
  .seed .icon { font-size: 19px; }
  .seed b { font-size: 11px; }
  .seed small { font-size: 9px; }
  .field-label { font-size: 14px; }
}

@media (min-width: 600px) and (max-width: 979px) {
  :root { --dock-space: calc(84px + env(safe-area-inset-bottom, 0px)); }
  .topbar { grid-template-areas: 'farmer resources next' 'account account account'; grid-template-columns: minmax(0, 1fr) minmax(230px, 1fr) auto; }
  .left-panel { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: center; }
  .tool-list { margin-top: 0; }
  .seed { min-height: 58px; }
  .tool, .market-shortcut { height: 58px; flex-direction: column; }
  .field-viewport { aspect-ratio: 1.8; }
}

/* A short landscape viewport needs a compact header and a fitted field.
   Enlarging still uses the same native scrolling viewport and 28 cells. */
@media (min-width: 600px) and (max-width: 979px) and (max-height: 500px) {
  :root { --landscape-field-height: max(150px, calc(100svh - 220px)); }
  .topbar {
    grid-template-areas: 'farmer resources next account';
    grid-template-columns: minmax(0, 1fr) minmax(160px, 1.2fr) auto auto;
    gap: 8px; padding: calc(8px + env(safe-area-inset-top, 0px)) 10px 8px;
  }
  .account-tools { display: flex; }
  #save-status { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .game-area { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(225px, 1fr); align-items: start; gap: 12px; }
  .field-heading { padding: 4px 10px 0; }
  .field-hint { display: none; }
  .field-viewport { height: var(--landscape-field-height); min-height: 0; max-height: none; aspect-ratio: auto; }
  .field-stage { padding-bottom: 12px; }
  .farm:not([data-zoomed='true']) #plots { width: min(100%, calc((var(--landscape-field-height) - 44px) * 2)); margin: 28px auto 0; }
  .right-panel { max-height: calc(100svh - var(--dock-space) - 88px); overflow: auto; padding: 12px; }
}
