/* ==========================================================================
   OpenTTD-style window chrome
   Shared stylesheet for the HTML prototypes and the WebTTD game UI.

   All sizes are 2x the original 640x480 OpenTTD UI, matching the reference
   screenshots. For 1x, wrap the window in `zoom: .5` (or set the `scale`
   option of the JS library).
   ========================================================================== */

@font-face {
  font-family: "OpenTTD Sans";
  src: url("OpenTTD-Sans.ttf") format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: block;
}

:root {
  --ttd-bg: #838583;
  --ttd-bevel-light: #a8a8a8;
  --ttd-bevel-dark: #626562;
  --ttd-caption-red: #c40000;
  --ttd-caption-blue: #1269c9;
  --ttd-ink: #101010;
  --ttd-white: #fcfcfc;
  --ttd-gold: #fcc000;
  --ttd-red: #fc0000;
  --ttd-input: #fcf880;
}

/* ---------- window shell ------------------------------------------------- */

.ttd-window,
.ttd-window *,
.ttd-window *::before,
.ttd-window *::after {
  box-sizing: border-box;
}

.ttd-window {
  display: flex;
  flex-direction: column;
  width: max-content;
  min-width: 160px;
  background: var(--ttd-bg);
  color: var(--ttd-white);
  font-family: "OpenTTD Sans", Tahoma, sans-serif;
  font-size: 20px;
  line-height: 20px;
  -webkit-font-smoothing: none;
  font-smooth: never;
  text-rendering: optimizeSpeed;
  user-select: none;
}

.ttd-window--floating {
  position: fixed;
  z-index: 100;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, .45);
}

.ttd-window--resizable {
  min-height: 96px;
}

.ttd-window__resize-handle {
  position: absolute;
  right: 1px;
  bottom: 1px;
  z-index: 5;
  width: 18px;
  height: 18px;
  background: repeating-linear-gradient(135deg, transparent 0 3px, var(--ttd-bevel-dark) 3px 4px, transparent 4px 7px);
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
  cursor: nwse-resize;
  touch-action: none;
}

.ttd-window__resize-handle:focus-visible {
  outline: 2px dotted var(--ttd-ink);
  outline-offset: -3px;
}

.ttd-window--dragging {
  cursor: grabbing;
}

/* ---------- title bar ---------------------------------------------------- */

.ttd-window__titlebar {
  display: flex;
  flex: 0 0 auto;
  height: 32px;
  cursor: default;
}

.ttd-window--floating .ttd-window__titlebar {
  cursor: move;
}

.ttd-window--dialog .ttd-window__titlebar {
  height: 28px;
}

.ttd-window__caption {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 0;
  padding: 0 10px 2px;
  overflow: hidden;
  white-space: nowrap;
  background: var(--ttd-company-caption, var(--ttd-caption-red));
  border: 2px solid;
  border-color: var(--ttd-bevel-light) var(--ttd-bevel-dark)
                var(--ttd-bevel-dark) var(--ttd-bevel-light);
  box-shadow: inset 2px 2px 0 var(--ttd-bevel-dark),
              inset -2px -2px 0 var(--ttd-bevel-light);
}

.ttd-window--dialog .ttd-window__caption {
  background: var(--ttd-bg);
}

.ttd-window--message .ttd-window__caption {
  background: var(--ttd-caption-blue);
}

.ttd-window__title {
  color: var(--ttd-white);
  text-shadow: 2px 2px 0 var(--ttd-ink);
}

.ttd-window__subtitle {
  color: var(--ttd-ink);
}

/* Title-bar buttons: raised 2px bevel, black UTF glyph. */
.ttd-titlebtn {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 32px;
  padding: 0;
  color: var(--ttd-ink);
  font: inherit;
  background: var(--ttd-bg);
  border: 2px solid;
  border-color: var(--ttd-bevel-light) var(--ttd-bevel-dark)
                var(--ttd-bevel-dark) var(--ttd-bevel-light);
  cursor: pointer;
}

.ttd-titlebtn--resize,
.ttd-titlebtn--shade,
.ttd-titlebtn--pin {
  width: 28px;
}

.ttd-window--dialog .ttd-titlebtn {
  height: 28px;
}

.ttd-titlebtn__icon {
  display: block;
  line-height: 1;
  transform: translateY(-1px);
  -webkit-text-stroke: .5px currentColor;
}

.ttd-titlebtn--close .ttd-titlebtn__icon {
  font-size: 30px;
  transform: translate(2.5px, 3.5px) scale(1.21, 1.1);
}

.ttd-titlebtn--resize .ttd-titlebtn__icon {
  font-size: 32px;
  -webkit-text-stroke-width: 1px;
}

/* The reference icon is a line above a small filled triangle. */
.ttd-titlebtn--shade .ttd-titlebtn__icon {
  position: relative;
  font-size: 15px;
  transform: translateY(1px);
}

.ttd-titlebtn--shade .ttd-titlebtn__icon::before {
  content: "";
  position: absolute;
  top: 0;
  left: calc(50% + .5px);
  width: 15px;
  height: 2px;
  background: currentColor;
  transform: translateX(-50%);
}

.ttd-window--shaded .ttd-titlebtn--shade .ttd-titlebtn__icon::before {
  top: auto;
  bottom: 0;
}

.ttd-titlebtn--pin .ttd-titlebtn__icon {
  font-size: 30px;
  transform: translate(-1px, 1px) scale(.97, .9);
}

.ttd-titlebtn:hover,
.ttd-btn:hover {
  background: #8b8d8b;
}

.open-ttd-directory-window .ttd-titlebtn:hover,
.open-ttd-directory-window .ttd-btn:hover,
.subsidy-directory-window .ttd-titlebtn:hover,
.subsidy-directory-window .ttd-btn:hover,
.company-directory-window .ttd-titlebtn:hover,
.company-directory-window .ttd-btn:hover,
.world-map-window .ttd-titlebtn:hover,
.world-map-window .ttd-btn:hover {
  background: color-mix(in srgb, var(--ttd-bg, #98845c), white 18%);
}

.ttd-titlebtn:active,
.ttd-titlebtn.is-active,
.ttd-btn:active,
.ttd-btn.is-active {
  background: #777977;
  border-color: var(--ttd-bevel-dark) var(--ttd-bevel-light)
                var(--ttd-bevel-light) var(--ttd-bevel-dark);
}

.ttd-titlebtn:focus-visible,
.ttd-btn:focus-visible,
.ttd-input__clear:focus-visible {
  outline: 2px dotted var(--ttd-ink);
  outline-offset: -4px;
}

/* ---------- body / panels ------------------------------------------------ */

.ttd-window__body {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.ttd-window--shaded {
  height: auto !important;
  min-height: 0 !important;
}

.ttd-window--shaded .ttd-window__body,
.ttd-window--shaded .ttd-window__buttons {
  display: none;
}

.ttd-raised {
  background: var(--ttd-bg);
  border: 2px solid;
  border-color: var(--ttd-bevel-light) var(--ttd-bevel-dark)
                var(--ttd-bevel-dark) var(--ttd-bevel-light);
}

.ttd-recessed {
  background: var(--ttd-ink);
  border: 2px solid;
  border-color: var(--ttd-bevel-dark) var(--ttd-bevel-light)
                var(--ttd-bevel-light) var(--ttd-bevel-dark);
}

/* ---------- text colours ------------------------------------------------- */

.ttd-text-white {
  color: var(--ttd-white);
  text-shadow: 2px 2px 0 var(--ttd-ink);
}

.ttd-text-gold {
  color: var(--ttd-gold);
  text-shadow: 2px 2px 0 var(--ttd-ink);
}

.ttd-text-red {
  color: var(--ttd-red);
  text-shadow: 2px 2px 0 var(--ttd-ink);
}

.ttd-text-black {
  color: var(--ttd-ink);
  text-shadow: none;
}

.ttd-num {
  text-align: right;
}

/* ---------- bottom button bar ------------------------------------------- */

.ttd-window__buttons {
  display: flex;
  flex: 0 0 auto;
  height: 28px;
}

.ttd-btn {
  display: flex;
  flex: 1 1 0;
  align-items: center;
  justify-content: center;
  min-width: 0;
  height: 28px;
  padding: 0 6px 2px;
  overflow: hidden;
  color: var(--ttd-ink);
  font: inherit;
  line-height: 20px;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: var(--ttd-bg);
  border: 2px solid;
  border-color: var(--ttd-bevel-light) var(--ttd-bevel-dark)
                var(--ttd-bevel-dark) var(--ttd-bevel-light);
  cursor: pointer;
}

.ttd-btn:disabled {
  color: #5d5f5d;
  cursor: default;
}

/* ---------- financial ledger -------------------------------------------- */

.ttd-ledger {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.ttd-ledger th,
.ttd-ledger td {
  height: 20px;
  padding: 0;
  vertical-align: top;
  font-weight: normal;
  text-align: right;
}

.ttd-ledger th:first-child,
.ttd-ledger td:first-child {
  width: 216px;
  text-align: left;
}

.ttd-ledger col.ttd-col-label { width: 216px; }
.ttd-ledger col.ttd-col-year  { width: 188px; }
.ttd-ledger col.ttd-col-gap   { width: 12px; }

.ttd-ledger__year {
  display: inline-block;
  margin-bottom: 14px;
  line-height: 20px;
  border-bottom: 2px solid var(--ttd-white);
}

.ttd-ledger td.ttd-ledger__indent {
  padding-left: 20px;
}

.ttd-ledger td.ttd-ledger__total {
  padding-right: 12px;
  text-align: right;
}

/* ---------- funds summary ------------------------------------------------ */

.ttd-funds {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.ttd-funds td {
  height: 20px;
  padding: 0;
  vertical-align: top;
}

.ttd-funds col.ttd-col-label { width: 193px; }
.ttd-funds col.ttd-col-value { width: 188px; }
.ttd-funds col.ttd-col-gap   { width: 58px; }
.ttd-funds col.ttd-col-info  { width: 363px; }

/* Keep these after the table rules so they also win inside .ttd-funds. */
.ttd-rule-row td {
  height: 4px;
  padding: 0;
  font-size: 0;
  line-height: 0;
}

.ttd-rule-row .ttd-rule-cell {
  border-top: 2px solid var(--ttd-ink);
}

.ttd-spacer-row td {
  height: 16px;
  font-size: 0;
  line-height: 0;
}

/* ---------- key/value statistics ---------------------------------------- */

.ttd-stat-row {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 20px;
}

.ttd-stat-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 6px;
  line-height: 20px;
}

/* ---------- text input --------------------------------------------------- */

.ttd-input-group {
  display: flex;
  width: 100%;
  height: 24px;
}

.ttd-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 24px;
  padding: 0 4px;
  color: var(--ttd-input);
  font: inherit;
  line-height: 20px;
  background: var(--ttd-ink);
  border: 2px solid;
  border-right: 0;
  border-color: var(--ttd-bevel-dark) var(--ttd-bevel-light)
                var(--ttd-bevel-light) var(--ttd-bevel-dark);
  outline: 0;
}

.ttd-input::selection {
  color: var(--ttd-ink);
  background: var(--ttd-white);
}

.ttd-input__clear {
  display: flex;
  flex: 0 0 34px;
  align-items: center;
  justify-content: flex-end;
  width: 34px;
  height: 24px;
  padding: 0;
  color: var(--ttd-ink);
  font: inherit;
  background: var(--ttd-bg);
  border: 2px solid;
  border-color: var(--ttd-bevel-light) var(--ttd-bevel-dark)
                var(--ttd-bevel-dark) var(--ttd-bevel-light);
  cursor: pointer;
}

/* OpenTTD's "clear" sprite is a framed cross. */
.ttd-input__clear-icon {
  position: relative;
  display: block;
  width: 26px;
  height: 16px;
  overflow: hidden;
  border: 2px solid currentColor;
}

.ttd-input__clear-icon::before,
.ttd-input__clear-icon::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 32px;
  background: currentColor;
  transform: translate(-50%, -50%) rotate(31deg);
}

.ttd-input__clear-icon::after {
  transform: translate(-50%, -50%) rotate(-31deg);
}

/* ---------- sprite helper (portraits, livery icons, ...) ----------------- */

.ttd-sprite {
  flex-shrink: 0;
  background-repeat: no-repeat;
  image-rendering: pixelated;
}

/* ---------- bottom message window --------------------------------------- */

.ttd-message-region {
  position: fixed;
  left: 50%;
  bottom: 36px;
  z-index: 200003;
  width: min(620px, calc(100vw - 16px));
  transform: translateX(-50%);
  pointer-events: none;
}

.ttd-message-region > .ttd-window {
  width: 100%;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, .45);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 120ms linear, transform 180ms ease-out;
  pointer-events: none;
}

.ttd-message-region > .ttd-window.is-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.ttd-message-window .ttd-window__body {
  gap: 4px;
  max-height: min(42vh, 260px);
  padding: 8px 12px 10px;
  overflow: auto;
}

.ttd-message-window__meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--ttd-bevel-light);
  font-size: 16px;
  line-height: 18px;
}

.ttd-message-window__headline {
  margin: 0;
  font: inherit;
  font-weight: bold;
  line-height: 22px;
}

.ttd-message-window__text {
  margin: 0;
  line-height: 22px;
}

.ttd-message-window__location {
  align-self: flex-start;
  flex: 0 0 auto;
  width: auto;
  min-width: 180px;
  margin-top: 4px;
}

@media (prefers-reduced-motion: reduce) {
  .ttd-message-region > .ttd-window {
    transition: none;
  }
}
