import SwiftUI import CoreText import AnalyticsKit /// Direction A, "Tide table" (picked Nov 24 2026): sea-glass ground, harbor-blue ink, one color per site. /// Fonts: Bricolage Grotesque for headings, Instrument Sans for text, IBM Plex Mono for figures, bundled in /// `Resources/Fonts` and registered by `Tide.registerFonts()` at launch. Full spec: design/DESIGN.md. public enum Tide { public static let ground = Color(light: 0xE6EDEB, dark: 0x0B1B21) public static let surface = Color(light: 0xF7FAF9, dark: 0x11252C) public static let sunk = Color(light: 0xDAE4E1, dark: 0x0A171C) public static let ink = Color(light: 0x0E2830, dark: 0xE2EEEC) public static let muted = Color(light: 0x557070, dark: 0x88A2A2) public static let line = Color(light: 0xC3D2CE, dark: 0x21404A) // Brand: the four colors of the Stacked tide icon. Every other token is tuned from these. public static let brandTide = Color(light: 0x1D6E8A, dark: 0x1D6E8A) public static let brandFoam = Color(light: 0xE6EDEB, dark: 0xE6EDEB) public static let brandLagoon = Color(light: 0x3BA49C, dark: 0x3BA49C) public static let brandAmber = Color(light: 0xC9853A, dark: 0xC9853A) /// Text, icons, focus rings and outlines. Tide blue in light mode; Lagoon in dark, where Tide is too dark to read. public static let accent = Color(light: 0x1D6E8A, dark: 0x5EC4BA) /// Filled controls (primary buttons, the selected sidebar row, switches): Tide blue in both modes, like the icon. public static let fill = Color(light: 0x1D6E8A, dark: 0x1D6E8A) public static let up = Color(light: 0x247A72, dark: 0x5EC4BA) // Lagoon public static let down = Color(light: 0xB0503A, dark: 0xE58A72) public static let surface2 = Color(light: 0xEDF3F1, dark: 0x163039) public static let ink2 = Color(light: 0x2F4C52, dark: 0xB4C8C7) public static let line2 = Color(light: 0xA9BDB8, dark: 0x2F5561) public static let control = Color(light: 0x7B9390, dark: 0x53747B) public static let accentStrong = Color(light: 0x175A71, dark: 0x86D6CD) public static let fillHover = Color(light: 0x175A71, dark: 0x247E9D) public static let fillPressed = Color(light: 0x18596F, dark: 0x195F77) public static let accentInk = Color(light: 0xFFFFFF, dark: 0xF2F7F6) // text on `fill` public static let accentTint = Color(light: 0xE1ECEE, dark: 0x173A3D) public static let accentTint2 = Color(light: 0xD0E1E5, dark: 0x1D4749) public static let onDown = Color(light: 0xFFFFFF, dark: 0x1A0B07) public static let downHover = Color(light: 0x934938, dark: 0xEA9F8B) public static let downPressed = Color(light: 0x7C4337, dark: 0xC47966) public static let downText = Color(light: 0x984A38, dark: 0xE58A72) public static let attention = Color(light: 0xC9853A, dark: 0xE3A45A) public static let attentionText = Color(light: 0x755B36, dark: 0xE3A45A) /// Quiet text inside the top bar pills: `muted` reaches only 4.1:1 on `sunk` in light mode. public static let pillText = Color(light: 0x485C5E, dark: 0x88A2A2) /// The selected range segment: `surface` disappears on `sunk` in dark mode. public static let pillSelected = Color(light: 0xF7FAF9, dark: 0x2A4651) public static let knob = Color(light: 0xFFFFFF, dark: 0xE2EEEC) public static let series: [Color] = [ Color(light: 0x1D6E8A, dark: 0x5DB6D2), Color(light: 0x3BA49C, dark: 0x59C9BF), Color(light: 0xC9853A, dark: 0xE3A45A), Color(light: 0x7A6CB0, dark: 0xA79AE0), Color(light: 0xC2566B, dark: 0xE57F93), Color(light: 0x6C8F3A, dark: 0x9DC266), Color(light: 0x8E9DA6, dark: 0x9FB0BA) ] /// The gray used for "Other sites" when a chart groups the long tail. public static let other = series[6] public static func color(for site: Site) -> Color { series[site.colorIndex % series.count] } // MARK: Type /// Registers the bundled fonts for this process. Call once at launch, before the first view draws. public static func registerFonts() { guard let urls = Bundle.module.urls(forResourcesWithExtension: "ttf", subdirectory: "Fonts"), !urls.isEmpty else { return } CTFontManagerRegisterFontURLs(urls as CFArray, .process, true, nil) } public enum Sans: String { case regular = "InstrumentSans-Regular", medium = "InstrumentSans-Medium", semibold = "InstrumentSans-SemiBold" } public static func sans(_ size: CGFloat, _ weight: Sans = .regular, relativeTo style: Font.TextStyle = .body) -> Font { .custom(weight.rawValue, size: size, relativeTo: style) } public static func display(_ size: CGFloat) -> Font { .custom("BricolageGrotesque-Bold", size: size, relativeTo: .largeTitle) } public static func figure(_ size: CGFloat, medium: Bool = false) -> Font { .custom(medium ? "IBMPlexMono-Medium" : "IBMPlexMono-Regular", size: size, relativeTo: .body).monospacedDigit() } public static let body = sans(14) #if os(iOS) static let phone = true #else static let phone = false #endif public static var screenTitle: Font { display(phone ? 32 : 28) } public static var kpi: Font { display(30) } public static var hero: Font { display(phone ? 44 : 34) } public static var cardTitle: Font { sans(phone ? 17 : 14, .semibold, relativeTo: .headline) } public static var text: Font { sans(phone ? 16 : 14) } public static var rowTitle: Font { sans(phone ? 16 : 13.5, .medium) } public static var secondary: Font { sans(phone ? 14 : 12.5, relativeTo: .subheadline) } public static var caption: Font { sans(phone ? 12 : 11.5, relativeTo: .caption) } public static var fieldLabel: Font { sans(phone ? 14 : 12, .semibold, relativeTo: .subheadline) } public static var sectionLabel: Font { sans(phone ? 12 : 10.5, .semibold, relativeTo: .caption) } public static var code: Font { figure(phone ? 13 : 12) } /// Full numbers below 100,000 ("6,284"), compact above ("128K"). public static func number(_ v: Double) -> String { abs(v) < 100_000 ? v.formatted(.number.precision(.fractionLength(0))) : v.formatted(.number.notation(.compactName)) } } public extension Color { init(light: UInt32, dark: UInt32) { #if os(macOS) self.init(nsColor: NSColor(name: nil) { $0.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua ? NSColor(hex: dark) : NSColor(hex: light) }) #else self.init(uiColor: UIColor { $0.userInterfaceStyle == .dark ? UIColor(hex: dark) : UIColor(hex: light) }) #endif } } #if os(macOS) extension NSColor { convenience init(hex: UInt32) { self.init(srgbRed: CGFloat(hex >> 16 & 0xff) / 255, green: CGFloat(hex >> 8 & 0xff) / 255, blue: CGFloat(hex & 0xff) / 255, alpha: 1) } } #else extension UIColor { convenience init(hex: UInt32) { self.init(red: CGFloat(hex >> 16 & 0xff) / 255, green: CGFloat(hex >> 8 & 0xff) / 255, blue: CGFloat(hex & 0xff) / 255, alpha: 1) } } #endif /// "+60%" in green, "−12%" in red, "new" when there is nothing to compare with. public struct DeltaLabel: View { let current: Double, previous: Double public init(current: Double, previous: Double) { self.current = current; self.previous = previous } public var body: some View { if previous <= 0 { Text(current > 0 ? "new" : "–").font(Tide.figure(12, medium: true)).foregroundStyle(Tide.muted) } else { let p = Int(((current - previous) / previous * 100).rounded()) // Ten times or more reads better as a multiple than as "+4000%". let text = current >= previous * 10 ? "×\(Int(current / previous))" : (p >= 0 ? "+\(p)%" : "−\(abs(p))%") Text(text).font(Tide.figure(12, medium: true)).foregroundStyle(p >= 0 ? Tide.up : Tide.down).lineLimit(1).fixedSize() } } } /// A number tile: label, big value, change vs the period before. With `selected`, it doubles as a tab. public struct KPITile: View { let label: String, value: Double, previous: Double, footer: String var selected: Bool? @State private var hover = false public init(_ label: String, value: Double, previous: Double, footer: String = "vs previous period", selected: Bool? = nil) { self.label = label; self.value = value; self.previous = previous; self.footer = footer; self.selected = selected } public var body: some View { let isSelected = selected == true VStack(alignment: .leading, spacing: 3) { Text(label).font(Tide.caption).foregroundStyle(isSelected ? Tide.accent : Tide.muted) Text(Tide.number(value)).font(Tide.kpi).foregroundStyle(Tide.ink).lineLimit(1).minimumScaleFactor(0.6) ViewThatFits(in: .horizontal) { HStack(spacing: 5) { DeltaLabel(current: value, previous: previous); Text(footer).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(1).fixedSize() } VStack(alignment: .leading, spacing: 1) { DeltaLabel(current: value, previous: previous); Text(footer).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(1).minimumScaleFactor(0.8) } } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) .padding(.vertical, 12).padding(.horizontal, 14) .background(isSelected ? Tide.surface : Tide.sunk, in: RoundedRectangle(cornerRadius: 10)) .overlay { RoundedRectangle(cornerRadius: 10) .strokeBorder(isSelected ? Tide.accent : (hover && selected != nil ? Tide.line2 : .clear), lineWidth: isSelected ? 1.5 : 1) } .onHover { hover = $0 } .accessibilityElement(children: .combine) .accessibilityAddTraits(isSelected ? .isSelected : []) } }