import SwiftUI

enum Tj {
    enum Palette {
        static let ink       = Color(red: 0.165, green: 0.153, blue: 0.137)
        static let ink2      = Color(red: 0.286, green: 0.275, blue: 0.251)
        static let inkSoft   = Color(red: 0.459, green: 0.447, blue: 0.424)
        static let sand      = Color(red: 0.976, green: 0.969, blue: 0.949)
        static let sand2     = Color(red: 0.929, green: 0.918, blue: 0.886)
        static let sand3     = Color(red: 0.878, green: 0.859, blue: 0.816)
        static let paper     = Color(red: 0.992, green: 0.988, blue: 0.973)
        static let line      = Color(red: 0.875, green: 0.863, blue: 0.831)
        static let lineSoft  = Color(red: 0.925, green: 0.918, blue: 0.890)
        static let text      = Color(red: 0.149, green: 0.137, blue: 0.118)
        static let text2     = Color(red: 0.420, green: 0.408, blue: 0.384)
        static let text3     = Color(red: 0.616, green: 0.604, blue: 0.580)
        static let brick     = Color(red: 0.886, green: 0.388, blue: 0.314)
        static let brickSoft = Color(red: 0.976, green: 0.863, blue: 0.824)
        static let amber     = Color(red: 0.871, green: 0.627, blue: 0.314)
        static let leaf      = Color(red: 0.180, green: 0.357, blue: 0.518)
        static let leafSoft  = Color(red: 0.867, green: 0.910, blue: 0.941)
        static let darkBg    = Color(red: 0.051, green: 0.063, blue: 0.059)
        // 数据折线主色:低饱和「桉叶 / 鼠尾草」青绿,沉静、契合健康、在米色背景上有高级感。
        // 替代原本近黑的 ink 折线;异常点仍用 brick 红点点缀,冷线 + 暖点对比清楚。
        static let teal      = Color(red: 0.337, green: 0.529, blue: 0.494)
        static let tealSoft  = Color(red: 0.808, green: 0.878, blue: 0.863)
        // 统一柔和阴影色:暖灰褐,低饱和。代替近黑的 ink 阴影,投影更柔、不发脏。
        static let shadow    = Color(red: 0.376, green: 0.345, blue: 0.298)
    }

    enum Radius {
        static let xs: CGFloat = 8
        static let sm: CGFloat = 14
        static let md: CGFloat = 20
        static let lg: CGFloat = 28
        static let xl: CGFloat = 36
        static let pill: CGFloat = 999
    }

    enum Shadow {
        static func card() -> some View {
            Color.clear
        }
    }
}

extension Font {
    /// 全 App 字体的唯一缩放出口。按全局档位 `appFontScale` 放大字号(老年/视力辅助)。
    /// 所有固定字号都经 `.system(size:)` → 机械迁移为 `.tjScaled(` 走这里;改档位 + 根视图重建即全局生效。
    static func tjScaled(_ size: CGFloat,
                         weight: Font.Weight = .regular,
                         design: Font.Design = .default) -> Font {
        .system(size: size * appFontScale, weight: weight, design: design)
    }

    static func tjTitle(_ size: CGFloat = 30) -> Font { .tjScaled(size, weight: .bold) }
    static func tjH2(_ size: CGFloat = 18) -> Font { .tjScaled(size, weight: .bold) }
    static func tjMono(_ size: CGFloat = 11) -> Font { .tjScaled(size, design: .monospaced) }
    static func tjSerifBody(_ size: CGFloat = 17) -> Font { .tjScaled(size) }
}

extension View {
    func tjCard(bordered: Bool = false, radius: CGFloat = Tj.Radius.md) -> some View {
        self
            .background(
                RoundedRectangle(cornerRadius: radius, style: .continuous)
                    .fill(Tj.Palette.paper)
            )
            .overlay(
                RoundedRectangle(cornerRadius: radius, style: .continuous)
                    .strokeBorder(Tj.Palette.lineSoft, lineWidth: bordered ? 1 : 0)
            )
            .shadow(color: bordered ? .clear : Tj.Palette.shadow.opacity(0.06),
                    radius: 2, x: 0, y: 1)
    }
}
